Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Decimal result not getting properly. Expand / Collapse
Author
Message
Posted Monday, September 09, 2013 11:48 PM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 5:54 AM
Points: 172, Visits: 450
Hi,
Below query displays result as 0.00 but i want to show as 0.73

SELECT ROUND(CAST (22/30 AS decimal (6,2)),-2);

Please suggest where m going wrong.


Thanks,
Abhas.
Post #1492996
Posted Tuesday, September 10, 2013 12:09 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Today @ 12:12 AM
Points: 2,200, Visits: 398
SELECT ROUND(CAST (22.0/30 AS decimal (6,2)),2);

1/ Sql considers 22 and 30 as integers, and so it performs an integer division. So the result is 0. Writing 22.0 force Sql to perform a decimal division.
2/ -2 as second parameter for ROUND force rounding to 100, not to 0.01

Roland
Post #1492999
Posted Tuesday, September 10, 2013 12:14 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Wednesday, April 16, 2014 5:54 AM
Points: 172, Visits: 450
Thanks Ronald,

select CAST(22 as decimal (6,2))/CAST(30 as decimal (6,2)) is also giving me output. :)
Post #1493000
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse