0

我有以下代码总是导致 0.00。我确定 weight_score 是一个实际数字。有人可以帮我格式化它,以便它产生一个十进制数。使用 SQL Server 2008,此代码在视图中。非常感谢!

CAST(SUM(weight_score * (45/100)) as decimal(10,2)) As avg_score
4

3 回答 3

9

45/100 被隐含地视为一个 INT 除以另一个 INT,这导致一个 INT 四舍五入为 0。为每个 INT 添加一个小数以解决该问题。

CAST(SUM(weight_score * (45.0/100.0)) as decimal(10,2)) As avg_score
于 2012-04-16T19:37:34.327 回答
0
 45/100 is treated as INT.So change it to 45.0/100.0. 
 select CAST(SUM(weight_score * (45.0/100.0)) as decimal(10,2)) As avg_score

 Even if you write the following code you will get 0
 select (45/100)
于 2012-04-16T19:54:30.390 回答
0

如果要避免错误,可以使用 NULLIF 函数。

于 2012-04-16T20:06:39.897 回答