Integer casting does not work correctly.
Your statement is incorrect!
In the CASE statement, you can only return one data type, so according to your expression you can return either INT or decimal(8,3) , since your case statement has decimal(8,3) , so here the INT data is implicitly converted in decimal! Please see below: always try to use the same return type in CASE to get the correct and expected result, thanks.
1.
select case @b when 1 then CAST(@a as int) -- return type INT when 2 then CAST(@a as int) -- return type INT end
2.
select case @b when 1 then CAST(@a as int) -- return type INT and then converted to decimal(8,3) when 2 then CAST(@a as decimal(8,3)) -- return type return type INT end
source share