摘要:
下文将通过举例的方式分享三种将空值值转换为0的方法分享
实验环境:sqlserver 2008 R2
下文将分别讲述使用sqlserver的三种函数进行空值替换:
isnull、case when 、coalesce 三种系统函数的方法处理空值,如下所示:
例:将表test中info列中的空值替换
create table test(keyId int identity, info varchar(30)) go insert into test(info)values('a'),('b'),(null),('d') go ---方法1:使用isnull替换 select keyId,isnull(info,0) as info from test go ---方法2:使用case when 替换 select keyId,case when info is null then 0 else info end as info from test ---方法3:使用coalesce替换相应的值 select keyId , coalesce(info,0) as info from test go truncate table test drop table test