select case when (len([name])=0 or [name] is null) and ([month] is not null or len([month])>0) then [month]
when (len([name])=0 or [name] is not null) and (len([month])=0 or [month] is null) then [name]
when (len([name])=0 or [name] is null) and ([month] is null or len([month])=0 ) then '0'
when (len([name])>0 and [name] is not null) and (len([month])>0 and [month] is not null) then [name]+';'+[month]
end from custom
上面从custom表中获取姓名和月份。
如果我们不做判断,直接用[name]+';'+[month],若其中有一个字段为null,则合并后仍为null。
所以要做判断。另外,对只含空格数据的字段还需做判断,因为空格也应该被当做没有数据,但实际sql没当成null,所以只有is null 还不行。len([name])=0就表示只含空格。