[英]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: 您有很多方法可以解决此问题,具体取决于您愿意承受的精度损失:
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
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.