【问题标题】:SQL- Convert Sum int to DecimalSQL- 将 Sum int 转换为 Decimal
【发布时间】:2020-10-14 19:25:52
【问题描述】:

我一直在尝试将此总和更改为小数。该计算为要在员工工作时间内完成的特定任务的数量设定了目标。结果就是问题,因为它继续以 int 形式给出结果。

((sum(IsNull([Employee Hours],0))/NullIf([ProcessTime],0))as decimal(6,2)) as [Planned Tasks],

我尝试了 CAST 和 CONVERT,但似乎没有任何效果,结果仍然显示为 int。

这是我尝试的最后一个脚本:

Cast((sum(IsNull([Employee Hours],0))/NullIf([ProcessTime],0))as decimal(6,2)) as [Planned Tasks],

非常感谢任何帮助。

【问题讨论】:

  • (1) 使用您正在使用的数据库进行标记。 (2) 提供样本数据和期望的结果。 (3) 解释为什么你的代码不工作。

标签: sql-server casting int decimal


【解决方案1】:

我猜问题不是数据类型而是结果。您的代码看起来像 SQL Server,并且 SQL Server 执行 integer 除法,因此 1/20,而不是 0.5。要获得小数,请转换为非整数——我倾向于乘以1.0

Cast(sum(coalesce([Employee Hours], 0) * 1.0 /
     NullIf([ProcessTime], 0) as decimal(6, 2)
    ) as [Planned Tasks],

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2011-05-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多