Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
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.

Examples:

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
jesse@jessemclain.com
www.jessemclain.com
http://jessesql.blogspot.com

Total article views: 2244 | Views in the last 30 days: 8
 
Related Articles
ARTICLE

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 ...

SCRIPT

convert string to ascii values

print every ascii value from a string

ARTICLE

Printing in .NET

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

FORUM

Printing Landscape

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

FORUM

string Comparison.

'string' = 'string___' ???

Tags
string formatting    
t-sql    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones