SQLTeam.com | Weblogs | Forums

Cast to decimal


#1

Hi,
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)

thanks
Regards
Nicole
:thinking:


#2

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!


#3

Great ! You save my life


#4

Glad to hear you hadn't actually jumped already!