sql server - Decimal precision and scale -
this expression uses implicit conversion:
select 9.999 * '9.999'
and evaluates 99.980001.
if log type information using this:
select sql_variant_property(9.999 * '9.999', 'basetype'), sql_variant_property(9.999 * '9.999', 'precision'), sql_variant_property(9.999 * '9.999', 'scale'), sql_variant_property(9.999 * '9.999', 'maxlength')
i get:
- basetype numeric
- precision 9
- scale 6
- maxlength 5
i believe understand results except precision. if number of digits left decimal point 2 , right 6, totals 8.
why sql server calculate 9 here?
p.s. use sql server 2008 r2
from precision, scale, , length (transact-sql)
looking @ section
operation: e1 * e2
you find
result precision p1 + p2 + 1
so in case 4+4+1 => 9
further that, notice that
select sql_variant_property(99.999 * '9.999', 'precision'), --11 => p1+p1+1 sql_variant_property(99.999 * '99.999', 'precision'), --11 => p1+p1+1 sql_variant_property(99.999 * 9.999, 'precision'), --10 => p1+p2+1 sql_variant_property(99.999 * 99.999, 'precision') -- 11 => p1+p2+1
whereas
select sql_variant_property(9.999 * '9.999', 'precision') --9 => p1+p1+1
and
select sql_variant_property(9.99 * '9.999', 'precision')
cuases
arithmetic overflow error converting varchar data type numeric.
Comments
Post a Comment