Thursday, February 24, 2005

int division in SQLServer T-SQL

Today, I'm trying to use T-SQL to do a calculation something like this


Declare @a intDeclare @b int
select @a=1, @b=2
select (@a/@b)*100

No the result was not 50, it was 0.Thats 'coz SQL Server performs an implicit data-type conversion and converts the resulting value of 1/2 as integer. Fix is to cast the operands to decimal

select ( cast(@a as decimal)/ cast(@b as decimal) )*100

No comments: