为什么SQL Server在将int转换成数据类型为numeric时抛出算法溢出错误?

时间:2021-11-05 15:50:20

I have an error being thrown by SQL Server Management Studio when running this code:

在运行此代码时,SQL Server Management Studio抛出一个错误:

declare @percentage numeric(3,2)
set @percentage = cast(15 as numeric(3,2))

but when I change numeric declaration to

但是当我改变数字声明的时候。

declare @percentage numeric(4,2)
set @percentage = cast(15 as numeric(4,2))

everything goes fine.

一切都很好。

Is there a limitation for numeric data type?

数值数据类型有限制吗?

4 个解决方案

#1


112  

Numeric defines the TOTAL number of digits, and then the number after the decimal.

数字定义数字的总数,然后是小数点后的数字。

A numeric(3,2) can only hold up to 9.99.

数字(3,2)最多只能保存9.99。

#2


26  

Lets see, numeric (3,2). That means you have 3 places for data and two of them are to the right of the decimal leaving only one to the left of the decimal. 15 has two places to the left of the decimal. BTW if you might have 100 as a value I'd increase that to numeric (5, 2)

允许看,数字(3 2)。这意味着数据有3个位置其中2个在小数点的右边只剩下1个在小数点的左边。15在小数点左边有两个位置。顺便说一下,如果你的值是100我就把它增加到数值(5,2)

#3


16  

NUMERIC(3,2) means: 3 digits in total, 2 after the decimal point. So you only have a single decimal before the decimal point.

数字(3,2)表示:总共3位,小数点后2位。所以小数点前只有一个小数。

Try NUMERIC(5,2) - three before, two after the decimal point.

尝试数字(5,2)- 3在前,2在小数点后。

#4


15  

Precision and scale are often misunderstood. In numeric(3,2) you want 3 digits overall, but 2 to the right of the decimal. If you want 15 => 15.00 so the leading 1 causes the overflow (since if you want 2 digits to the right of the decimal, there is only room on the left for one more digit). With 4,2 there is no problem because all 4 digits fit.

精确和规模常常被误解。在数字(3,2)中,您需要3位数字,但是小数点右边2位。如果你想要15 => 15.00,那么前边的1就会导致溢出(因为如果你想要小数点右两位,那么左边就只剩下一个数字了)。对于4,2,没有问题,因为所有的4位都匹配。

#1


112  

Numeric defines the TOTAL number of digits, and then the number after the decimal.

数字定义数字的总数,然后是小数点后的数字。

A numeric(3,2) can only hold up to 9.99.

数字(3,2)最多只能保存9.99。

#2


26  

Lets see, numeric (3,2). That means you have 3 places for data and two of them are to the right of the decimal leaving only one to the left of the decimal. 15 has two places to the left of the decimal. BTW if you might have 100 as a value I'd increase that to numeric (5, 2)

允许看,数字(3 2)。这意味着数据有3个位置其中2个在小数点的右边只剩下1个在小数点的左边。15在小数点左边有两个位置。顺便说一下,如果你的值是100我就把它增加到数值(5,2)

#3


16  

NUMERIC(3,2) means: 3 digits in total, 2 after the decimal point. So you only have a single decimal before the decimal point.

数字(3,2)表示:总共3位,小数点后2位。所以小数点前只有一个小数。

Try NUMERIC(5,2) - three before, two after the decimal point.

尝试数字(5,2)- 3在前,2在小数点后。

#4


15  

Precision and scale are often misunderstood. In numeric(3,2) you want 3 digits overall, but 2 to the right of the decimal. If you want 15 => 15.00 so the leading 1 causes the overflow (since if you want 2 digits to the right of the decimal, there is only room on the left for one more digit). With 4,2 there is no problem because all 4 digits fit.

精确和规模常常被误解。在数字(3,2)中,您需要3位数字,但是小数点右边2位。如果你想要15 => 15.00,那么前边的1就会导致溢出(因为如果你想要小数点右两位,那么左边就只剩下一个数字了)。对于4,2,没有问题,因为所有的4位都匹配。