首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

sqlServer中Sum函数碰到null不做了?该怎么解决

2012-02-19 
sqlServer中Sum函数碰到null不做了?在SQLServer2000中测试如下:如一表Table ,A,b,c,d字段A字符,b,c,d为int

sqlServer中Sum函数碰到null不做了?
在SQLServer2000中测试如下:
如一表Table ,A,b,c,d字段
A字符,b,c,d为int

Select a,sum(b+c+d) as e
from Table1
group by a

如果,其中一个为NULL,则系统计算结果也为NULL,即b,c,d中一个为NULL,则sum(b+c+d) 结果为NULL
这是为何?怎么避免此问题?


[解决办法]
Select a,sum(isnull(b,0)+isnull(c,0)+isnull(d,0)) as e
from Table1
group by a

[解决办法]
在SQL 里NULL 与任何字符 数字 相加减都为null

Select a,sum(isnull(b,0)+isnull(c,0)+isnull(d),0) as e 
from Table1 
group by a
[解决办法]
Select a,sum(isnull(b,0)+isnull(c,0)+isnull(d,0)) as e 
from Table1 
group by a
[解决办法]
用isnull即可

热点排行