27

Guys i want to print the following data with comma separated values using oracle sql

696585242087


to 

69,658,524,2087

and same for the decimal.

1
  • 3
    Why would you want a comma before the right most 4 digits? Commented Jan 24, 2011 at 15:34

4 Answers 4

31
SELECT  TO_CHAR(696585242087, '99G999G999G9999', 'NLS_NUMERIC_CHARACTERS=",."')
FROM    dual
Sign up to request clarification or add additional context in comments.

2 Comments

+1 Allows for countries that use a decimal comma
What if I don't know how many digits the source number have, I mean is there any pattern to group every 3 digits regardless of number's length?
30

See the Oracle docs for all the insanity that can be done for number formatting. The short version is you need the "TO_CHAR" function, and provide a formatting string for the output:

TO_CHAR( col_name, '999,999,999,999') 

Should do what you need.

Comments

2

take a look to to_char: http://www.oradev.com/oracle_number_format.jsp

Comments

2

Taking the Dead programmer's update (24/Jan/2011) (original author of the question) and the comment of Amir Pashazadeh (5/Mar/2016), we can get the desired comma separated format with the following example.

This code applies for numbers with equal or less than 15 digits, if you will need for bigger length, you should add more 999,999 according with the max of length you will manage.

SELECT TRIM(TO_CHAR(12345678, '999,999,999,999,999')) ttry FROM DUAL
UNION ALL
SELECT TRIM(TO_CHAR(    1234, '999,999,999,999,999')) ttry FROM DUAL
;

Result:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.