分裂时为什么我一直保持0

时间:2012-04-16 19:36:44

标签: sql sql-server sql-server-2008 casting sum

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

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

3 个答案:

答案 0 :(得分:9)

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

CAST(SUM(weight_score * (45.0/100.0)) as decimal(10,2)) As avg_score

答案 1 :(得分: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)

答案 2 :(得分:0)

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