Blog Pages

Different place of casting --> different results


declare @intA int = 2, @intB int = 5

select @intA / @intB * 100.00 -- = ?
select @intA * 100.00 / @intB -- = ?


Why are the results different?

In the first select - the cast to decimal number is done after the first operation - that it int:
2/5 = 0.4 = 0 (int!) * 100.00 = 0.00 (cast the zero to decimal)

In the first select - the cast to decimal is in the first operation, so the other operations will be decimal too:
2 * 100.00 = 200.00 (decimal!) / 5 = 40.00

No comments:

Post a Comment