Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL to_char Currency Formatting

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?

like image 465
Law Avatar asked Aug 28 '14 09:08

Law


People also ask

How do I display currency format in SQL?

In SQL Server, you can use the T-SQL FORMAT() function to format a number as a currency. The FORMAT() function allows you to format numbers, dates, currencies, etc. It accepts three arguments; the number, the format, and an optional “culture” argument.

What does TO_CHAR do in SQL?

TO_CHAR(expression,format) converts a date, time, timestamp (date and time), or number expression to a character string according to the specified format string.

What can we use instead of TO_CHAR in SQL?

SQL Server has separate functions for YEAR() , MONTH() , and DAY() . Oracle uses TO_CHAR() ; SQL Server uses CONVERT() . One option is to define the functions YEAR() , MONTH() , and DAY() in Oracle and then use string concatenation (via the CONCAT() ) function to combine the data.

Can you format numbers in SQL?

The FORMAT() function formats a number to a format like "#,###,###. ##", rounded to a specified number of decimal places, then it returns the result as a string.


2 Answers

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

like image 165
Captain Avatar answered Oct 14 '22 22:10

Captain


This can be used as well since the decimal notation is already know for French countries

SELECT TO_CHAR(1000000000,'999,999,999,999.99') "Amount"  FROM DUAL;
like image 25
Solami Avatar answered Oct 14 '22 22:10

Solami