简体   繁体   English

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

[英]Error: Arithmetic overflow error converting numeric to data type numeric

I'm getting error: 我收到错误消息:

Arithmetic overflow error converting numeric to data type numeric on below code. 在以下代码上将数字转换为数据类型数字的算术溢出错误。

declare @A as   nvarchar(100)   --will be Nvarchar only
    declare @T as   nvarchar(100)     --will be Nvarchar only
    declare @Act as nvarchar(100) --will be Nvarchar only
    set @A ='750000'
    set @T ='552000'

    SELECT  @Act = cast(cast(cast(@A  AS decimal(10, 4)) -cast( @T AS decimal(10, 4)) 
    /cast(@T AS decimal(10, 4)) AS decimal(10, 4)) * 100 AS numeric(10, 4))

How to get ride of this error? 如何获得这个错误?

First of all, why are you using the wrong datatype and make things hard for you, you should always use the proper datatype. 首先,为什么要使用错误的数据类型并使您的工作变得困难,所以应始终使用正确的数据类型。

So instead of VARCHAR why don't use DECIMAL , still here is the query since your question is How to get ride of this error 因此,为什么不使用DECIMAL而不是VARCHAR ,仍然是查询,因为您的问题是如何解决此错误

declare @A as   nvarchar(100)   --will be Nvarchar only
    declare @T as   nvarchar(100)     --will be Nvarchar only
    declare @Act as nvarchar(100) --will be Nvarchar only
    set @A ='750000'
    set @T ='552000'


SELECT @Act = CAST(
                    (
                      CAST(@A AS DECIMAL(10,4))-
                      CAST(@T AS DECIMAL(10,4))
                    ) /
                    (CAST(@T AS DECIMAL(10,4)) * 100)
                  AS VARCHAR);
SELECT @Act

No need to cast AS numeric(10, 4) and you need to cast to VARCHAR since you are assign the value to a varchar variable, not a numeric one. 不需要强制转换为AS numeric(10, 4)并且由于必须将值分配给varchar变量而不是数字变量,因此您需要强制转换为VARCHAR

Your query will be easy if you change the datatypes, eg: 如果更改数据类型,则查询将很容易,例如:

DECLARE @A DECIMAL(10,4) = 750000,
        @T DECIMAL(10,4) = 552000,
        @Act VARCHAR(100);

SELECT @Act = CAST((@A - @T) / (@T * 100) AS VARCHAR(100))

SELECT @Act

Or if you want to cast the result as numeric(10, 4) 或者,如果您要将结果强制转换为numeric(10, 4)

DECLARE @A DECIMAL(10,4) = 750000,
        @T DECIMAL(10,4) = 552000,
        @Act VARCHAR(100);

SELECT @Act = CAST(CAST((@A - @T) / (@T * 100) AS numeric(10, 4)) AS VARCHAR(100))

SELECT @Act

You need to increase the precision value higher. 您需要提高精度值。 Try to change like this: 尝试这样更改:

declare @A as   nvarchar(100)   --will be Nvarchar only
    declare @T as   nvarchar(100)     --will be Nvarchar only
    declare @Act as nvarchar(100) --will be Nvarchar only
    set @A ='750000'
    set @T ='552000'

    SELECT  @Act = cast(cast(cast(@A  AS decimal(18, 2)) -cast( @T AS decimal(18, 4)) 
    /cast(@T AS decimal(18, 4)) AS decimal(18, 4)) * 100 AS numeric(18, 4))

    print @Act

It is giving error because 它给错误,因为

The entire length of the number you have provided is greater than ten. 您提供的电话号码的总长度大于10。

You can change your code as shown below 10, 4 to 18, 4 . 您可以如下10, 418, 4所示更改代码。

SELECT  @Act = cast(cast(cast(@A  AS decimal(10, 4)) -cast( @T AS decimal(10, 4)) 
    /cast(@T AS decimal(10, 4)) AS decimal(18, 4)) * 100 
    AS numeric(18, 4))

It's value is more than the max limit so it is giving exception. 它的值大于最大限制,因此它给出了异常。

If I understand your logic correctly, what you want to do with: 如果我正确理解您的逻辑,那么您想做什么:

SELECT @Act = 
    cast(
        cast(
            cast(@A  AS decimal(10, 4)) - 
            cast(@T AS decimal(10, 4)) / cast(@T AS decimal(10, 4)) 
        AS decimal(10, 4)) 
        * 100
    AS numeric(10, 4))

is this simple calculation: 这是简单的计算:

SELECT @Act = (@N1 - @N2/@N2) * 100

Then, in your case, you need to cast @Act as numeric(12, 4) , because you multiple by 100. Your result will be: 74999900.0000 . 然后,在您的情况下,您需要将@Actnumeric(12, 4) @Act numeric(12, 4) ,因为您要乘以100。结果将是: 74999900.0000

I'm not sure, but I think that you probably want to calculate: 我不确定,但我认为您可能要计算:

SELECT @Act = (@N1 - @N2)/@N2 * 100

Even then, as a rule, always consider the range of your math calculations and use appropriate numeric data type for your variables. 即使这样,通常也要始终考虑数学计算的范围,并对变量使用适当的数字数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM