1

I want to convert decimal number to specific format.

FROM 123456789.1234 to 12,34,56,789.00.

I tried it using

SELECT FORMAT(123456789.1234, 'N', 'en-us') as 'en-us'

but this returns:

123,456,789.12 

I also tried

SELECT FORMAT(123456789.1234, '##,###.00') as 'en-us'

and got the same result.

Any suggestions on this?

6
  • do you really mean this format 12,34,56,789.00? maybe you mean 123,456,789.00. Commented Feb 28, 2018 at 5:55
  • 4
    OK, I'll say it ... Formatting really belongs in the presentation layer. Commented Feb 28, 2018 at 6:14
  • ##\,##\,##\,###\.\0\0? Commented Feb 28, 2018 at 6:29
  • @shawnt, if number is 123 only, above format will show somthing ,,123.00. Commented Feb 28, 2018 at 9:52
  • @John Woo, I need 12,34,56,789.00 only Commented Feb 28, 2018 at 9:53

5 Answers 5

1

Use ROUND OR FLOOR function with FORMAT function -

SELECT FORMAT(ROUND(123456789.1234,0), 'N', 'en-IN') as 'Round',
       FORMAT(FLOOR(123456789.1234), 'N', 'en-IN') as 'Floor'

Please check difference between Round and Floor function.

Sign up to request clarification or add additional context in comments.

3 Comments

Was waiting for someone to offer something other than round()
@BhatiaAshish - round or floor is not my primary goal. My goal is to achieve number separators. above solution gives me - Round 1234,56,789.00,Floor 1234,56,789.00 , which did not help me.
@NishantRana, Above query gives me 12,34,56,789.00 which is as per requirement. Not sure why this not working on your machine.
0

The proper way to do this would probably be Round()

SELECT FORMAT(ROUND(123456789.1234,0), 'N', 'en-us') as 'en-us'

Comments

0

Just try round() in inner side. you also want "12,34,56,789.00" format. So, for that you can change regional setting of your machine.

Comments

0

If, you don't want to use decimal values you could simply cast them as INT/BIGINT

SELECT FORMAT(cast(123456789.1234 as bigint), 'N', 'en-In') 

Use culture parameter en-In for Indian Numbering system instead of Western Numbering system en-us look for Numbering system

4 Comments

This doen't help me exactly with my requirement. I need to show 12,56,789. Above is showing me 1234,56,789
@NishantRana.. what SQL Version u r using ?
SQL Server 2012
@NishantRana... make sure your culture parameter has value of en-In instead of en-us.
0
replace(ltrim(format(d, '## ## ## ## ###\.\0\0')), ' ', ',')

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.