SQL to_char Currency Formatting

Law picture Law · Aug 28, 2014 · Viewed 40.7k times · Source

I am facing a to_char() currency formatting problem here.

The below code is working for me:

SELECT TO_CHAR(10000,'L99G999D99MI',
               'NLS_NUMERIC_CHARACTERS = ''.,''
               NLS_CURRENCY = $') "Amount"
  FROM DUAL; 

which will provide me with the output: $10,000.00.
Now, I want to convert the currency into a France currency, which the desire output is 10 000,00 or a Switzerland currency with output 10'000.00. So, I modified the code as shown below for both of the case above:

SELECT TO_CHAR(10000,'L99G999D99MI',
               'NLS_NUMERIC_CHARACTERS = ''"", ""''
               NLS_CURRENCY = ''$'' ') "Amount"
  FROM DUAL;


SELECT TO_CHAR(10000,'L99G999D99MI',
               'NLS_NUMERIC_CHARACTERS = ''". "''
               NLS_CURRENCY = ''$'' ') "Amount"
  FROM DUAL;

But this code does not work and showing an error of ORA-12702. Is there any problem with the code?

Answer

Captain picture Captain · Aug 28, 2014

If you want to do it in the query:

SELECT TO_CHAR(10000,'L99G999D99MI',
           'NLS_NUMERIC_CHARACTERS = ''.''''''
           NLS_CURRENCY = ''$'' ') "Amount"
           FROM DUAL;

Gives $10'000.00 (as this string is getting pre-processed there are pairs of quotes around the characters (becoming single) and then to get a single-quote in the string you need four quotes to become one!)

SELECT TO_CHAR(10000,'L99G999D99MI',
           'NLS_NUMERIC_CHARACTERS = '', ''
           NLS_CURRENCY = ''$'' ') "Amount"
           FROM DUAL;

Gives $10 000,00