Powered By Blogger

Thursday, November 5, 2009

Thousand seperators

Slightly bizarre.. after years of running queries through Oracle basic editors and being able to provide primitive formatting I found SQL server unable to do something (easily) that I thought would be trivial. If I return a count(*) from a table and want to send that information to someone I'd expect to be able to simply add thousand seperators and set the decimal places etc.

In Oracle I'd simply type:

SELECT TO_CHAR (COUNT ( * ), '999,999,999') AS row_count
FROM MyTable;

but in SQL server I find myself jumping through hoops and end up with:

select convert(varchar, CAST(COUNT(*) as money),1) as row_count
from MyTable;

Converting to money?? This seems to be recommended technique on the SQL forums. You still need another step to strip out the decimal places that money will give you as well.

Guess you could argue that you would likely format at the Excel or SSRS stage but should really be able to do this more simply in SSMS.