Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Convert float to comma varchar with decimals

I have a couple floats that are kinda big. They're around a 100 million.

I would like this number to show like the following 123,456,789.01234

I've found that I can use CONVERT if its a money datatype but this doesn't do the full trick (it leaves off some decimal places).

I have to have commas on the left and five decimal places on the right.

Is there any built in SQL function to help with this? Or do I have to write a custom function?

Thanks

*** Update

  • I forgot to mention that I'm just displaying these as varchars. So there isn't any calculations after this.
  • This is running on an SQL database so MySQL and Oracle won't work.
like image 631
Miles Avatar asked Mar 14 '26 13:03

Miles


1 Answers

DECLARE @f FLOAT

SET @f = 123456789.01234

SELECT  LEFT('$' + CONVERT(VARCHAR(20), CAST(@f AS MONEY), 1), LEN(@f) - 2)

this will cut it up to two places of decimal for formatting. You can change LEN(@f) - 2 to modify this setting.

like image 142
Learning Avatar answered Mar 16 '26 06:03

Learning



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!