【问题标题】:How to check if real has any digits after floating point如何检查浮点数后real是否有任何数字
【发布时间】:2021-10-30 02:31:18
【问题描述】:

我有一张包含多个实数的表格。它看起来像这样:

ParticipantId GWF_Score
123 185
456 193,5
789 80

我已经构建了一个存储过程,它以 XML 格式返回这个表。

我必须像表格中写的那样打印分数。

如果我这样写 convert(decimal, GWF_Score) as [@points], 我会得到 185,194,80

如果我这样写 cast(GWF_Score as decimal(10,2)) as [@points], 我会得到 185.00, 193.50, 80.00

如果我这样写 (case when GWF_Score % 1 != 0 then cast(GWF_Score as decimal(10,2)) else convert(decimal, GWF_Score)end) as [@points], 我会收到一条错误消息,指出 real 和 int 与模运算符不兼容。

【问题讨论】:

  • 我建议在您的表示层而不是在数据库中执行此操作。数据库用于保存数据。您的表示层用于格式化。
  • 这看起来像是您的表示层的工作,不是 RDBMS。

标签: sql tsql casting type-conversion


【解决方案1】:

请试试这个:

(case when GWF_Score % 1.0 != 0.0 then cast(GWF_Score as decimal(10,2)) else convert(decimal, GWF_Punkte)end) as [@points]

【讨论】:

  • "数据类型实数和数值在模运算符中不兼容。"
猜你喜欢
  • 1970-01-01
  • 2012-09-10
  • 2013-02-25
  • 2016-06-18
  • 2011-04-22
  • 2023-03-14
  • 1970-01-01
相关资源
最近更新 更多