日期:2014-05-18  浏览次数:20581 次

去掉多个字段显示出来的NULL的方法,很头疼啊,总是不好用。。。
唉, 惆怅啊,大神救命。。。

------解决方案--------------------
SQL code

ISNULL(字段名,‘’)

------解决方案--------------------
isnull(col,'') as col
就可以去掉null
------解决方案--------------------
SQL code

select *  
from (select IsNull(a.charge_id,0)as charge_id ,
IsNull(a.charge_name,0)as charge_name,
IsNull(d.division_name,0)as division_name,
IsNull(e.dept_name,0)as dept_name,
IsNull(f.section_name,0)as section_name,
IsNull(b.post_name,0)as post_id,
IsNull(a.post_id,0)as post_id,
IsNull(c.division_id,0)as division_id,
IsNull(c.dept_id,0)asdept_id,
IsNull(c.section_id ,0)as  section_id
from charge_mst a left join post_mst b on a.post_id = b.post_id inner join cddsc_trn c on c.charge_id = a.charge_id left join division_mst d on c.division_id = d.division_id  
and c.division_id <> '0' left join dept_mst e on c.division_id = e.division_id  
and c.dept_id = e.dept_id  
and c.dept_id <> '0' left join section_mst f on c.division_id = f.division_id  
and c.dept_id = f.dept_id  
and c.section_id = f.section_id  
and c.section_id <> '0') as g  
where 1=1

------解决方案--------------------
case when 也可以
------解决方案--------------------
可以用isnull()函数,比如地址字段,是字符串类型,当地址没有记录时可以用isnull(地址字段名,'--'),当地址没有数据时,就用--代替,就不显示null了。当然--可以换成你想要的代替内容。如果是数据类型,如年龄字段,就不能加引号了,如年龄没有记录时,可以这样写isnull(年龄字段名,0),就会用0代替null了。