关于 numeric(38,36) 类型的乘法问题
declare @y numeric(38,36), @x numeric(38,36)
select @y=0.263998462275450127016764362727123456
select @x=0.963998462275450127016764362727654321
select @y+@x --1
select @y*@x --2
select 0.263998462275450127016764362727123456*0.963998462275450127016764362727654321 --3
结果:
1.227996924550900254033528725454777777
0.254494111676617352767868706335420
0.2544941116766173527678687063354196236
问题:第二个结果为何不等于第三个结果?
------解决方案--------------------参与2和3计算的数据的精度不一样导致的.
楼主改成这样:
declare @y numeric(36,36), @x numeric(36,36)
select @y=0.263998462275450127016764362727123456
select @x=0.963998462275450127016764362727654321
select @y+@x --1
select @y*@x --2
select 0.263998462275450127016764362727123456*0.963998462275450127016764362727654321 --3
------解决方案--------------------上面的运算结果就是一致的了.
常数可以借助 SQL_VARIANT_PROPERTY 来获得 sql server 将其识别后的数据类型(包括精度)
例如:
SELECT
Type = SQL_VARIANT_PROPERTY(0.263998462275450127016764362727123456, 'basetype '),
[Precision] = SQL_VARIANT_PROPERTY(0.263998462275450127016764362727123456, 'Precision '),
[scale] = SQL_VARIANT_PROPERTY(0.263998462275450127016764362727123456, 'scale ')