Friday, June 22, 2007

Small example of decimal result of numeric operation in SQL server

Hello

here is the small example to get result of numeric operation in decimal in sql server

declare @a decimal(10,2)
declare @a1 int
declare @a2 int

set @a1 = 3
set @a2 = 2
set @a = @a1/@a2
print @a

this will give "1.00". which is wrong result...

to get correct result you just need to cast one of the integer value in decimal as shown below

declare @a decimal(10,2)
declare @a1 int
declare @a2 int

set @a1 = 3
set @a2 = 2

set @a = @a1/cast(@a2 as decimal)
print @a

this will give "1.50". which is the result we are looking for.

Enjoy!

No comments: