sql - 小数除以小数并得到零

标签 sql sql-server math decimal division

为什么我这样做:

select CAST(1 AS DECIMAL(38, 28))/CAST(1625625 AS DECIMAL(38, 28))

我得到 0,但是当我得到

select CAST(1 AS DECIMAL(20, 10))/CAST(1625625 AS DECIMAL(20, 10))

我得到一个值?

我本来期望更高精度的除法会返回更高精度的结果,为什么不是这样?

最佳答案

发生这种情况是因为对于数学运算,结果精度和比例是根据 http://msdn.microsoft.com/en-us/library/ms190476.aspx#division 中的规则计算的

operation
e1 / e2

result precision
p1 - s1 + s2 + max(6, s1 + p2 + 1)

result scale
max(6, s1 + p2 + 1)

因此,当对两个部分都使用 decimal(38/28) 时,您将以 decimal(105,67) 结尾,这不受支持,因此它会被截断。

也引用

The result precision and scale have an absolute maximum of 38. When a result precision is greater than 38, the corresponding scale is reduced to prevent the integral part of a result from being truncated.

关于sql - 小数除以小数并得到零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21169231/

相关文章:

sql - Postgres ON CONFLICT 设置列引用不明确

sql - 体面的可视化 SQL 查询生成器

SQL Server 2005 搜索某些数据库对象的 View

math - float 学有问题吗?

mysql - 表中每个组的底部 N 行的列的平均值

mysql - mysql 表连接和按 id 分组

c# - 将 XML 发送到 sp 以进行插入

c# - 尝试部署 dapac : The path specified by File Ggroup FG. mdf 文件不在有效目录中时,SQL Linux docker 容器抛出错误

c++ - 如何以非常初级的水平在 C++ 中显示四分之一圆?

objective-c - 正态分布函数