且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-27 16:38:27

阅读本文:

http://msdn.microsoft.com/en-us/library/ms187746.aspx [ ^ ]



NUMERIC(p,s): p表示小数点左边和右边



一般来说,在同一个上下文中不能有大数字和小数字(处理数百万
Read this :
http://msdn.microsoft.com/en-us/library/ms187746.aspx[^]

NUMERIC (p,s) : p means both left and right of the decimal

Generally you can't have large numbers and small numbers with in the same context (handling millions of


并跟踪1/10000美分等因为它们在这些尺度上没有任何差别。)
's and keeping track of 1/10000 of cents etc. as it would not make any difference at those scales).


给出的解决方案1 Mehdi Gholam 非常好。



我想添加以下内容,因为OP想要明确知道为什么在第一种情况下没有抛出错误以及为什么在第二种情况下抛出错误。 />


从陈述中可以看出

The Solution 1 given by Mehdi Gholam is very good.

I want to add the following since the OP wants to know specifically why error was not thrown in the first case and why error is thrown in the second case.

As seen from the statements
SELECT CAST(1234567891234567.34 AS NUMERIC(18,2))  --Works
SELECT CAST(1234567891234567.34 AS NUMERIC(18,10)) --fails with Arithmetic overflow error



很明显,在第一种情况下,语句 NUMERIC(18,2)小数点左边有18位和16位数字,而 1234567891234567 是16位数。因此,没有错误。

在第二种情况下,从语句 NUMERIC(18,10)开始,左边有8位数字十进制,但 1234567891234567 是16位数,超过8位数。因此,发生算术溢出错误。


it is clear that, in the first case from the statement NUMERIC(18,2) the total digits are 18 and 16 digits are available to the left of decimal, whereas 1234567891234567 are 16 digits. Hence, there is no error.
In the second case, from the statement NUMERIC(18,10), 8 digits are available to the left of decimal, but 1234567891234567 are 16 digits which is more than 8 digits. Hence, Arithmetic overflow error occurs.