Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Get your favorite SSC scripts directly in SSMS with the free SQL Scripts addin. Search for scripts directly from SSMS, and instantly access any saved scripts in your SSC briefcase from the favorites tab.
Download now (direct download link)

Insert Commas Into Number String

By Jesse McLain,

This function will add commas to a given number passed as a string, separating thousands, millions, etc. Rather than doing long division to figure out how many "digit groups" there are, it simply reverses the number string, adds the commas after every third digit (after the decimal place, if present), and then re-reverses the string for return.


PRINT dbo.fn_AddCommasToNumberString('20123.12') -- 20,123.12
PRINT dbo.fn_AddCommasToNumberString('2012312')  -- 2,012,312
PRINT dbo.fn_AddCommasToNumberString('23')       -- 23

Written by Jesse McLain

Total article views: 2291 | Views in the last 30 days: 1
Related Articles

Trouble printing out long VARCHAR(MAX) strings?

Ever try to print out a long VARCHAR(MAX)/NVARCHAR(MAX) string, only to find that it's truncated at ...


Print Long String

Quite often, I output information through PRINT command. It works well only when message is shorter ...


convert string to ascii values

print every ascii value from a string


Printing in .NET

A bit of a break from the SQL Server side with this great new .NET class developed by new author Jer...


Printing Landscape

label printing, zebra s4m 200pdi, pdf, direct print

string formatting