SQL Server 转换因算术溢出而失败

发布于 2024-09-02 15:05:04 字数 318 浏览 4 评论 0原文

根据 SQL Server 2008 联机丛书中关于小数和数字数据类型的条目,精度为:

p(精度) 可存储的小数位数的最大总数(包括小数点左侧和右侧)。精度必须是 1 到最大精度 38 之间的值。默认精度为 18。

但是,下面的第二个选择失败,并显示“将 int 转换为数据类型 numeric 时出现算术溢出错误”。

SELECT CAST(123456789 as decimal(9,0))
SELECT CAST(123456789 as decimal(9,1))

According to the entry for decimal and numeric data types in SQL Server 2008 Books Online, precision is:

p (precision)
The maximum total number of decimal digits that can be stored, both to the left and to the right of the decimal point. The precision must be a value from 1 through the maximum precision of 38. The default precision is 18.

However, the second select below fails with "Arithmetic overflow error converting int to data type numeric."

SELECT CAST(123456789 as decimal(9,0))
SELECT CAST(123456789 as decimal(9,1))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

朦胧时间 2024-09-09 15:05:04

请参阅此处: http://msdn.microsoft.com/ en-us/library/aa258832(SQL.80).aspx

十进制[(p[, s])]

p(精度)指定小数位数的最大总数
可以存储,都在左侧
和小数点右边。
精度必须是从 1 开始的值
通过最大精度。这
最大精度为 38。默认值
精度为 18。

s(小数位数) 指定可显示的最大小数位数
存储在小数点右边
观点。比例必须是从 0 开始的值
通过 p.只能指定比例
如果指定了精度。默认
比例尺为 0;因此,0 <= s <= p。
最大存储大小有所不同,具体取决于
精度。

使用:decimal(p,s)时,将p视为您要存储的总位数(无论小数点左边还是右边),并且< code>s 为小数点右侧应有多少个 p 数字。

DECIMAL(10,5)=     12345.12345
DECIMAL(10,2)=  12345678.12
DECIMAL(10,10)=         .1234567891
DECIMAL(11,10)=        1.1234567891

您的示例代码失败:

SELECT CAST(123456789 as decimal(9,1))

因为:

9=精度(小数点左右的位数)
1=scale(小数点右边的总位数)
(9-1)=8(小数点左边的总位数)

,您的值 123456789 需要小数点左边 9 位。您将需要 decimal(10,1) 或仅 decimal(9,0)

see here: http://msdn.microsoft.com/en-us/library/aa258832(SQL.80).aspx

decimal[(p[, s])]

p (precision) Specifies the maximum total number of decimal digits
that can be stored, both to the left
and to the right of the decimal point.
The precision must be a value from 1
through the maximum precision. The
maximum precision is 38. The default
precision is 18.

s (scale) Specifies the maximum number of decimal digits that can be
stored to the right of the decimal
point. Scale must be a value from 0
through p. Scale can be specified only
if precision is specified. The default
scale is 0; therefore, 0 <= s <= p.
Maximum storage sizes vary, based on
the precision.

when using: decimal(p,s), think of p as how many total digits (regardless of left or right of the decimal point) you want to store, and s as how many of those p digits should be to the right of the decimal point.

DECIMAL(10,5)=     12345.12345
DECIMAL(10,2)=  12345678.12
DECIMAL(10,10)=         .1234567891
DECIMAL(11,10)=        1.1234567891

your sample code fails:

SELECT CAST(123456789 as decimal(9,1))

because:

9=precision (total number of digits to left and right of decimal)
1=scale (total number of digits to the right of the decimal)
(9-1)=8 (total digits to the left of the decimal)

and your value 123456789 requires 9 digits to the left of the decimal. you will need decimal(10,1) or just decimal(9,0)

め可乐爱微笑 2024-09-09 15:05:04

正确的。由于您正在执行 decimal(9,1) 这意味着您总共有 9 位数字,但是 ,1 保留了其中一位作为小数点右侧,所以你最多可以向左做 8 次,向右做 1 次。

Correct. Since you're doing decimal(9,1) that means you have 9 total digits, but the ,1 is reserving one of them for the right of the decimal place, so you can do at most 8 to the left and 1 to the right.

梦里兽 2024-09-09 15:05:04

尝试
选择 CAST(123456789 作为十进制(10,1))

try
SELECT CAST(123456789 as decimal(10,1))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文