tsql - Sql Server significant digits -


i have variable want return max of 5 left of decimal , 3 right

convert(varchar(14),convert(decimal(8,3),         datediff(millisecond,@processtime,getdate()))/@onethousand) 

now variables defined

declare @processtime datetime  declare @onethousand decimal(8,3) set @onethousand = 1000.000 

this returns like

0.003000000000 

i guess can solve problem left function, question why happen @ all. if minimum variable of decimal defined, @onethousand, 3 shouldn't return value have 3 decimals?

the result of division of decimal(8,3) decimal(8,3)gives datatype of decimal(20,12) see this

declare @processtime datetime =getdate() declare @onethousand decimal(8,3) set @onethousand = 1000.000  declare @v sql_variant  set @v = convert(decimal(8,3),         datediff(millisecond,@processtime,getdate()))/@onethousand  select     cast(sql_variant_property(@v, 'basetype') varchar(30)) basetype,     cast(sql_variant_property(@v, 'precision') int) precision,     cast(sql_variant_property(@v, 'scale') int) scale 

the bol section explains why decimal(20,12) is here

operation:        e1 / e2 result precision: p1 - s1 + s2 + max(6, s1 + p2 + 1) result scale:     max(6, s1 + p2 + 1) 

so in case

result precision: 8 - 3 + 3 + max(6, 3 + 8 + 1) = 20 result scale:     max(6, 3 + 8 + 1)             = 12 

Comments