August 3, 2006 at 9:46 am
I have the following view which just multiplies two fields and then divides by 100 to give me my result. Result should be 3.65. However, the view is returning 3 even. I need the decimal places to be returned. I have tried to cast and convert to a decimal without much luck. Is there a way to get my desired result or to make it quit rounding?
SELECT SUM(weight * points) / 100) AS Total
FROM dbo.table
Thanks for any help.
August 3, 2006 at 9:58 am
The datatype of the result of an expression is dependant on the data types used in it. Either explicitly CAST() them, or tack on a decimal portion:
SELECT SUM(weight * points) / 100.0) AS Total
or
SELECT SUM(weight * points) / CAST(100 As numeric(10,2))
August 3, 2006 at 9:58 am
SELECT SUM(weight * points) / 100.00) AS Total
FROM dbo.table
August 3, 2006 at 10:05 am
Thanks for the quick reply. While both answers at least get me a decimal, it is rounding to only 3.7. I need it to round to 3.65. I tried both solutions you provided, both with the 3.7 result. My result of the weight * points is 365 and / 100 should give me 3.65. It just appears to still be rounding, only up this time.
Any other ideas?
August 3, 2006 at 10:24 am
SELECT SUM( CAST(weight * points AS DECIMAL(10,2)) / CAST(100.00 AS DECIMAL(10,2)) )
August 3, 2006 at 6:44 pm
Read topic "converting data types, overview" in BOL.
Probably it will give you same idea how to deal with precisions in SQL.
_____________
Code for TallyGenerator
August 3, 2006 at 6:57 pm
Get 'em Serqiy!
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 7 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply