Connecting Tech Pros Worldwide Forums | Help | Site Map

Round to 2 decimal

Newbie
 
Join Date: Jan 2008
Location: Kochi
Posts: 13
#1: Jul 30 '08
I wrote an Sql as :-

SELECT CAST(ROUND(SUM(CASE Question11 WHEN 1 THEN 1 ELSE 0 END), 0) AS numeric) * 100 /
(SELECT COUNT(*)
FROM CustomerFeedback
WHERE (Question11 <> 0)) AS Expr1
FROM CustomerFeedback

The output is 66.6666........................

I want the output to display in 2 decimal place ie., 66.66 or 66.67

Can you help me....

code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,083
#2: Jul 30 '08

re: Round to 2 decimal


I think the problem is you are rounding then casting as numeric where you haven't specified a format.
You probably don't need the CAST here. Change
Expand|Select|Wrap|Line Numbers
  1. SELECT CAST(ROUND(SUM(CASE Question11 WHEN 1 THEN 1 ELSE 0 END), 0) AS numeric)
to
Expand|Select|Wrap|Line Numbers
  1. SELECT ROUND(SUM(CASE Question11 WHEN 1 THEN 1 ELSE 0 END),2)
or specify a numeric format
Expand|Select|Wrap|Line Numbers
  1. SELECT CAST(ROUND(SUM(CASE Question11 WHEN 1 THEN 1 ELSE 0 END), 0) AS numeric(8,2)
ck9663's Avatar
Expert
 
Join Date: Jun 2007
Posts: 1,925
#3: Jul 30 '08

re: Round to 2 decimal


If it's for display purposes, you can handle that in your front-end.

-- CK
Reply