SQLTeam.com | Weblogs | Forums

Cast to decimal


I jump right out the window:

select cast(100/7920 as decimal(20,4))
= 0.0000

it shout be: 0,01262

what's wrong with this query. (SQL2008R2)



The 100/7920 is calculated first and it is INTEGER arithmetic and THEN the CAST to decimal is done.

Do this instead:

select cast(100/7920.0 as decimal(20,4))

(make either one, or both, have a decimal place to force decimal arithmetic OR cast them to DECIMAL before the maths.

Come Back! You jumped too soon!


Great ! You save my life


Glad to hear you hadn't actually jumped already!