Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Simple decimal issue

Author  Topic 

sqlslick
Yak Posting Veteran

83 Posts

Posted - 2014-02-10 : 13:55:08
Hey guys,

This is probably a dumb question but 3133232 / 1048576 = 2.9880828857421875. If you execute SELECT 3133232 / 1048576 the result is 2. How can I get it to display 2.98? I've tried the code below but that generates 2.00.

SELECT CAST(3133232 / 1048576 AS DECIMAL (10, 2))

Thanks in advanced!

sqlslick
Yak Posting Veteran

83 Posts

Posted - 2014-02-10 : 14:08:46
Nevermind, SELECT 3133232 / 1048576.0 did the trick. Just have to use the ROUND() function now for precision.
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2014-02-10 : 14:19:07
yeah, when performing division in sql server the scale and precision of the denominator determines the scale and precision of the result. Then if you want to round the result that is a separate operation. Your first statement resulted in an integer because that is what your denominator was. and casting an int to decimal(10,2) just results in zeros to the right of the decimal.

Be One with the Optimizer
TG
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2014-02-11 : 00:49:03
quote:
Originally posted by sqlslick

Nevermind, SELECT 3133232 / 1048576.0 did the trick. Just have to use the ROUND() function now for precision.


see
http://beyondrelational.com/modules/2/blogs/70/posts/10825/beware-of-implicit-conversions.aspx

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -