Tuesday, 17 October 2006

Play around with T-SQL decimal and float data types

Decimal data type in t-sql is an interesting thing. It automatically trunc data decimal part in a division operation if precision part is not defined. I have this query:

declare @result decimal
select @result = 2 / 10
select @result

The result is 0.

if I change @result to float, the result is 0.0.

To get what we need for a floating point value, we could do this:

declare @result float
select @result = cast(2 as float) / cast(10 as float)
select @result

Then the result is 0.200000000001

No comments: