6

Goal

Format a COUNT result to a common format (e.g. ###,##0) in a concise statement and without UDF's.

I've Tried

I am currently using something like this, though it leaves two decimals and is clunky:

SELECT CONVERT(VARCHAR, CAST(COUNT([id]) AS MONEY), 1) FROM tbl

The reason I went that direction is because it was the only standard formatting option I could find when reading through the CAST and CONVERT documentation from MSDN.

I don't really like it, but it limps along since these numbers are simply copied and pasted into other text editors and such after the T-SQL runs. They do of course have to be formatted by hand to remove the decimals in some places.

Research and Restrictions

Of course you could build a user-defined function, like this one on this blog, but I have a restriction that keeps me from building UDF's for this purpose.

After some additional research I found that if I were using SQL 2012 I could use a new T-SQL FORMAT function, alas, I'm restricted to 2008 R2. This of course leverages a different platform as it's a .NET interface. :D

I am also aware of this solution: Format a number with commas but without decimals in SQL Server 2008 R2?

7
  • I'm sure you're aware of this, and I don't want to question anyone with so much rep, but this really is handled so much easier in your application, unless you're absolutely hell-bent on doing it in your SQL query Aug 15, 2013 at 18:35
  • 2
    Why don't you perform this formatting on the client, e.g. C# has very good functionality in Format() and ToString(). Also please stop declaring varchar without length. It's lazy and dangerous. Aug 15, 2013 at 18:35
  • @LittleBobbyTables, I'm restricted to T-SQL in this case. I would, as you stated, do it in .NET if I had the luxury. However, this is a massive conversion script that must be executed on the SQL server because of restrictions out of my control. Aug 15, 2013 at 18:36
  • @AaronBertrand, I'm restricted to using T-SQL in this case. I would love to, of course, leverage the .NET framework here to make my life a little easier. If I could do the entire conversion using .NET I would, but alas, I've been restricted by the powers that be. Aug 15, 2013 at 18:39
  • 1
    So what is wrong with the existing solution? You want something more concise? More efficient? Something else? If it ain't broke, I don't think you're going to find anything "better" other than formatting on the client. Aug 15, 2013 at 18:48

2 Answers 2

13

Even though the question author was stuck on SQL Server 2008R2, this is a concise and efficient solution should you be blessed with SQL Server 2012 or newer.

No digits after decimal point (rounded)

SELECT FORMAT(COUNT([id]), N'N0')
FROM [tbl]

Example output

53,234,568

One digit after decimal point

SELECT FORMAT(COUNT([id]), N'N1')
FROM [tbl]

Example output

53,234,567.9

Two digits after decimal point

SELECT FORMAT(COUNT([id]), N'N2')
FROM [tbl]

Example output

53,234,567.89

(etc.)

Two digits after decimal point, no thousands separator

SELECT FORMAT(COUNT([id]), N'#.00')
FROM [tbl]

Example output

53234567.89
2
  • 2
    This should be answer.
    – DtechNet
    Mar 1, 2019 at 21:07
  • 1
    This is the best answer! May 4, 2021 at 16:21
9

Not the most elegant, but you can remove the trailing .00 with replace.

SELECT REPLACE(CONVERT(VARCHAR, CAST(COUNT([id]) AS MONEY), 1), '.00', '')
FROM tbl
2
  • 2
    +1, my initial thought was to use LEFT(zzz,len(zzz)-3), but this seams better
    – KM.
    Aug 15, 2013 at 19:35
  • I do this with count(*) but it converts the result in a totally different number
    – ffuentes
    Jun 14, 2022 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.