This evaluation in sql doesn’t seems to work properly for some reason but i can’t figure out why.
Lets take this example first.
DECLARE @countRatio decimal(6,3)
SET @countRatio = (((4)/(2))*(6))/100.0
_______________________________________
0,120
This evaluation works like a charm. I also get the same answer with my calculator.
But this example give me an unexpected answer.
DECLARE @countRatio decimal(6,3)
SET @countRatio = (((2)/(4))*(6))/100.0
_______________________________________
0,000
When i calculate this on my calculator the answer is 0,03
which acording to me is correct. But the sql keeps giving me 0,000 as the answer. Any ideas?
The problem is that
((2)/(4))is rounded down to anINTsince 2 and 4 are bothINTs. If you instead use 2.0 or 4.0 you get the right result:In any arithmetic expression, SQL Server casts to the type of the operator that has the greater precision. So
2.0/4casts the result to the type of2.0, which isfloat.