【问题标题】:Arithmetic overflow error converting numeric to data type numeric in sql在sql中将数字转换为数据类型数字的算术溢出错误
【发布时间】:2017-06-24 16:16:56
【问题描述】:

我有一个这样的代码查询。

set @CurrentPatchDeployPercentage = convert(numeric(5,2), (isnull(@LastMonthDeployCount, 0)* 100.00 / isnull(nullif(@TotalMachines, 0), 1)))

但是当我运行时出现以下错误,请帮助。

将数字转换为数据类型数字的算术溢出错误

【问题讨论】:

  • numeric(5,2) 允许总共 5 个数字,2 个小数表示 5 - 2 = 左边的总共 3 个数字。您的计算结果超过 999.99 并引发错误。增加 numeric() 的大小
  • 您可以尝试将 numeric(5,2) 增加到 numeric(x,y),其中 x,y 相对高于您指定的值
  • 大家好,我将值增加到 numeric(6,3) 但仍然是同样的错误

标签: sql-server sql-server-2008


【解决方案1】:

您确定这就是您想要获得百分比的方式吗?你应该在这样划分之后移动你的*100.00吗?

rextester:http://rextester.com/NNOEH44668

declare @LastMonthDeployCount numeric(7,2) = 10.00;
declare @TotalMachines numeric (7,2) = 100.00;

declare @CurrentPatchDeployPercentage numeric(9,2);
    set @CurrentPatchDeployPercentage = 
      convert(numeric(5,2), 
        ( isnull(@LastMonthDeployCount, 0)
          / 
          isnull(nullif(@TotalMachines, 0), 1.0)
          ) * 100.00
        );

select @CurrentPatchDeployPercentage;

另外,请确保您的@CurrentPatchDeployPercentage 数据类型可以支持@LastMonthDeployCount *100.00 的最大数量,当@TotalMachines = 0 更改为null 然后更改为1

Decimal/Numeric Data Type - MSDN

Precision  Storage bytes
---------  --------------
1 - 9      5
10-19      9
20-28      13
29-38      17

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多