简体   繁体   English

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

[英]Arithmetic overflow error converting numeric datatype to numeric

I'm having following columns in a table 我在表中有以下各列

SIZE NUMERIC(14,5)    
PRICE NUMERIC(14,5)    

when I perform this select query, 当我执行此select查询时,

SELECT SIZE,
PRICE,
SIZE*PRICE AS TOTAL 
FROM TNAME

I'm getting results: 我正在得到结果:

1.00000 90.00000    90.0000000000
1.00000 90.00000    90.0000000000
1.00000 90.00000    90.0000000000
1.00000 100.00000   100.0000000000
1.00000 30.00000    30.0000000000

I'm wondering why the third column is returning with 10 digits after decimal point? 我想知道为什么第三列以小数点后的10位返回?

Also I'm getting 我也得到

Arithmetic overflow error converting numeric datatype to numeric 将数字数据类型转换为数字的算术溢出错误

while inserting result into another table which has the same columns with same datatype 在将结果插入另一个具有相同列和相同数据类型的表中时

INSERT INTO TNAME2(SIZE, PRICE, TOTAL)
   SELECT 
       SIZE, PRICE, SIZE * PRICE AS TOTAL 
   FROM 
       TNAME

Try This. 尝试这个。

SELECT SIZE,
PRICE,
CAST(SIZE*PRICE AS numeric(14,5))AS TOTAL 
FROM TNAME
INSERT  INTO TNAME2
SELECT    SIZE ,
Price ,
CAST(SIZE * PRICE AS NUMERIC(15, 5))
FROM      TNAME

OR 要么

INSERT  INTO TNAME2 (SIZE,Price,TOTAL)
SELECT    SIZE ,
Price ,
CAST(SIZE * PRICE AS NUMERIC(15, 5)) AS TOTAL
FROM      TNAME

Regarding the first question, the number of decimals, there is nothing wrong. 关于第一个问题,小数位数,没有错。 It's the basic, everyday multiplication we learn at school: Multiplying two decimal numbers produces a number with as many decimal digits as the sum of decimal digits of the operands. 这是我们在学校学习的基本的日常乘法:将两个十进制数字相乘会产生一个数字,该数字的十进制数字与操作数的十进制数字之和一样多。 The number of integer digits can be up to the sum of integer digits of the operands. 整数位数最多可以等于操作数的整数位数之和。

Multiplying 10.11 by 12.13 produces 122.6343 . 10.11乘以12.13 122.6343 It would be VERY awkward if SQL Server broke this basic rule and arbitrarily truncated the result. 如果SQL Server违反了此基本规则并任意截断了结果,那将非常尴尬。

As a result, when you try to store the product in a column that accepts fewer digits, you get an overflow error. 结果,当您尝试将产品存储在接受较少数字的列中时,会出现溢出错误。 SQL Server won't change the number of digits automatically because there is no way to decide the correct action. SQL Server不会自动更改位数,因为没有办法确定正确的操作。

There are a lot of ways you can handle this, depending on the loss of precision you are willing to suffer: 您有很多方法可以解决此问题,具体取决于您愿意承受的精度损失:

  • Truncate the extra digits, ie throw them away, accepting up to a unit loss. 截断多余的数字,即丢弃它们,接受最多单位损失。 This can become a LOT of money if you store totals. 如果您存储总计,这可能会变成很多钱。
  • Round to the desired number of digits. 四舍五入到所需的位数。 Sounds intuitive, but what about half-way values, ie 0.00005 in your case? 听起来很直观,但是中间值(在您的情况下为0.00005)又如何呢? Should it be 0.0001 or 0.0000? 是0.0001还是0.0000? So we have 所以我们有
  • Rounding up, where 0.5 becomes 1, resulting in up to .5 loss per entry 四舍五入,其中0.5变为1,导致每次输入最多损失0.5
  • Down, when it becomes 0, with the same loss 向下,当它变​​为0时,损失相同
  • Round to even or odd, where you round to the nearest odd or even number, which on average produces minimal loss. 四舍五入到偶数或奇数,即四舍五入到最接近的奇数或偶数,这平均会产生最小的损失。 While this sounds weird, it is the standard defined in IEEE 754. It's also called banker's rounding because it's used in bookkeeping to minimize losses due to truncation. 虽然这听起来很奇怪,但它 IEEE 754中定义的标准。它也被称为banker's rounding因为它在簿记中用于最大程度地减少由于截断而造成的损失。

If you want to store fewer digits you need to decide whether you need to truncate the extra digits or how to round the number, then do it yourself in code. 如果要存储较少的数字,则需要确定是否需要截断多余的数字或如何对数字进行四舍五入,然后自己编写代码。

In your case, you can use CAST to a numeric of the desired precision. 在您的情况下,可以将CAST用作所需精度的数字。 This will perform rounding half up, where 0.00005 becomes 0.0001, eg: 这将执行四舍五入,其中0.00005变为0.0001,例如:

INSERT INTO TNAME2(SIZE, PRICE, TOTAL)
SELECT 
   SIZE, PRICE, CAST(SIZE * PRICE as numeric(14,5)) AS TOTAL 
FROM 
   TNAME

SQLFiddle here SQLFiddle 在这里

This will work, assuming the number of digits doesn't exceed 14, otherwise you will have to change the type of your table field. 假设数字位数不超过14,这将起作用,否则,您将不得不更改表格字段的类型。

If you want some other kind of rounding in SQL, you will have to create your own function. 如果要在SQL中进行其他四舍五入,则必须创建自己的函数。

Got below useful link. 在下面找到有用的链接。 Multiplication of numerics As per this link you can try as query as below 数字的乘法按照此链接,您可以尝试如下查询

SELECT SIZE,
PRICE,
CAST(SIZE*PRICE AS numeric(28,5))AS TOTAL 
FROM TNAME

Try this 尝试这个

SELECT SIZE,PRICE,CONVERT(numeric(14,5), SIZE*PRICE) AS TOTAL 
FROM TNAME

Write the same query in insert, it must work 在插入中写相同的查询,它必须工作

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

相关问题 错误:将数字转换为数据类型数字的算术溢出错误 - Error: Arithmetic overflow error converting numeric to data type numeric 在执行查询时将数值转换为数值类型的算术溢出错误 - Arithmetic overflow error converting numeric to data type numeric on query execution 在插入时将数字转换为数据类型数字的算术溢出错误 - Arithmetic overflow error converting numeric to data type numeric on insert 将数值转换为数值类型的算术溢出错误 - Arithmetic overflow error converting numeric to data type numeric 将数字转换为数据类型数字的算术溢出错误 - Arithmetic overflow error converting numeric to data type numeric SQL 8115将数值转换为数据类型numeric的算术溢出错误 - SQL 8115 Arithmetic overflow error converting numeric to data type numeric 将数字转换为数据类型数字的算术溢出错误 - Arithmetic overflow error converting numeric to data type numeric 将数值转换为数值类型的算术溢出错误 - Arithmetic overflow error converting numeric to data type numeric 算术溢出错误将数字转换为数据类型数字 - Arithmetic overflow error converting numeric to data type numeric Varchar数据类型中的小数数据-将varchar转换为数值数据类型的算术溢出错误 - Decimal Data in Varchar datatype - Arithmetic overflow error converting varchar to data type numeric
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM