views:

48

answers:

3

I have a stored procedure that queries some data and emails it, I'd like to change the procedure so that any numbers in the thousands get displayed with commas like 1,000. I guess the best way of doing this in SQL is to convert it to a string and add the comma?

Thanks

+1  A: 

Correct. An integer has no formatting by definition of the data type.

So you must either convert the value to a string(varchar for example) or as is the more common practice, have the presentation tier handle the formatting of the value.

John Sansom
In this case there is no presentation tied as the stored procedure if firing an email with the data.
Gavin Draper
A: 

If you're stuck using SQL Server as the presentation of the email (no other report application),

SELECT CONVERT(varchar(24),CONVERT(money,CAST( 12345678 AS VarChar(24))),1)

Jeff O
Is there a way of doing this without the decimal places?
Gavin Draper
See @Strommy answer.
Jeff O
+2  A: 

Here's a relatively clean answer. Convert to money (no data type loss). Then convert to varchar with style 1 (it does the commas for you) and then remove the .00 from the end, since it comes with the money data type.

select REPLACE(CONVERT(VARCHAR, CONVERT(MONEY, 1234), 1), '.00', '')

It's a fine line to figure out how many string manipulations before doing an inline function, but this should be a pretty quick conversion.

Thanks, Eric

Strommy