Sunday, March 11, 2012

Division in T-SQL

Hi,

Can anyone tell me why this doesn't return any decimals?


declare @.f float
set @.f = 6 / 18
print @.f

All i get is 0... Is there something obvious i've done wrong?

TIAhmmm...okay, it works when I add .0 at the end of one of the numbers.

Like: 18.0 or 6.0

Still doesn't make sense, though... Why does the query engine assume i want an integer answer when the variable is a float?|||you need to xplicitly convert them...


declare @.f float
set @.f = convert(float,6) / convert(float,18)
print @.f

hth|||The evaluation is done before the assignment and the engine does not look ahead to figure out what assumptions to make. If I type 12 the engine will assume integer.

No comments:

Post a Comment