tsql - SQL Server decimal variable assignment? -
i'm trying write stored procedure , i'm getting unexpected divide 0 exceptions.
i've narrowed down following example.
why in world :
declare @a decimal; declare @b decimal; declare @c decimal; set @a = 4; set @b = 9; set @c = @a/@b select @a/@b 'expected' select @c 'wut'
result in this?
expected --------------------------------------- 0.4444444444444444444 (1 row(s) affected) wut --------------------------------------- 0 (1 row(s) affected)
the problem haven't specified scale decimal
type. msdn:
s (scale)
the number of decimal digits stored right of decimal point. number substracted p determine maximum number of digits left of decimal point. scale must value 0 through p. scale can specified if precision specified. the default scale 0; therefore, 0 <= s <= p.
so when try store @a/@b
@c
, fractional part gets truncated.
notice:
declare @a decimal(18, 3); declare @b decimal(18, 3); declare @c decimal(18, 3); set @a = 4; set @b = 9; set @c = @a/@b select @a/@b -- 0.44444444444444444444 select @c -- 0.444
Comments
Post a Comment