2012-04-16 100 views
0

我有以下总是导致0.00的代码片段。我确定weight_score是一个实际的数字。有人可以帮我格式化它,结果是十进制数。使用SQL Server 2008和此代码在视图中。非常感谢!为什么我在分割时总是得到0

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

回答

9

45/100被隐式视为INT由另一INT,这导致被四舍五入为0十进制添加到每个变通,一个INT划分。

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

太棒了!工作 – dido 2012-04-16 19:47:08

+0

@dido:很高兴帮助。请记住尽可能地接受这个答案。 – 2012-04-16 20:03:46

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) 
0

如果你想避免的错误,你可以使用NULLIF功能。

相关问题