0

I am using below statement in Microsoft SQL Select statement and the result i am getting is like "100.00000000000" however i would like it to be as "100.00".. i.e. Only two trailing zeros after period. Can some one tell what i am missing here and how to fix it

round(Cast(count(Case when Newuser = 1 then 'Yes' end)as decimal) / count(Totalusers),4)*100 as '% New Users''
Jens
  • 67,715
  • 15
  • 98
  • 113
Tayyab
  • 65
  • 6
  • 1
    possible duplicate of [Remove trailing zeros from decimal in SQL Server](http://stackoverflow.com/questions/2938296/remove-trailing-zeros-from-decimal-in-sql-server) – Leonid Jan 26 '15 at 06:42
  • I asked a similar question once, and gbn provided a great answer here: http://dba.stackexchange.com/questions/41743/automatic-decimal-rounding-issue . So it's not all that easy to tell where the "trailing" zeroes start, and the accurracy of the actual number ends. However, if you *always* want to round for instance percentages (max 100) to two decimals, you can just display the end result like so: CAST(ROUND(MY_END_RESULT_COL, 2) AS DEC(5,2)) – Kahn Jan 26 '15 at 06:48
  • I have tried use the Cast as String but it has removed the trailing zeros from whole numbers .. and the output is like 100, 95.96, 50, 50.95 however i want it to be 100.00, 96.96, 50.00, 50.95 – Tayyab Jan 26 '15 at 10:33

1 Answers1

0

This is a matter of representation as a string / varchar, right? I always use convert to numeric and round. So: CONVERT(NUMERIC(16,desired_nr_of_decimals), ROUND(decimal_value, desired_nr_of_decimals))

M.Stoop
  • 119
  • 2