TIL: How to Add Commas to Numeric Output

One thing that’s always driven me crazy is when I have large numbers in my resultsets and the lack of commas for readability. For a lot of different things I do, the more commas a number has, the more attention I want to give to a given value.

Image
Image

When blown up like this, these are easier to read… but when I have hundreds of values that I want to skim, not so much. I’d much prefer to have my used_space also only display 2 decimal places… it’s less noise and I don’t need any more precision for what I’m doing. I did not feel like doing something like CAST(column AS DECIMAL(10, 2)) to force the decimal places, and it still would not get me the commas I wanted.

Out of annoyance, I decided to search to see if someone had a simple UDF out there that could solve my problems. And lo and behold, the heavens opened up as I got an AI generated response in addition to my usual search results…

Image

Mind… blown!!!

I had sort of remembered when FORMAT() came out, but will confess that I never really bothered looking into it. I knew it could do date and time manipulation, but had no idea it could add comma separators AND limit decimal places!!!

Image
Image

Note ‘N2’ for two decimal places and ‘N0’ (zero) for no decimal places.

Moral of this story – it’s never too late to learn something new.