连接语句时多个case的结果

时间:2013-08-13 06:56:35

标签: tsql sql-server-2005

我希望在满足条件时显示在when语句时构建的多个字符串的串联。如下:

select

case
  when T1.Field is not null then 'T1,' 
  when T2.Field is not null then /*last results*/ + 'T2,' 
  when T3.Field is not null then /*last results*/ + 'T3,'
end


from T1 
left outer join T2 on ...
left outer join T3 on ...

2 个答案:

答案 0 :(得分:4)

为什么不尝试使用ISNULL

这样的东西
ISNULL(T1.Field,'') + ISNULL(T2.Field,'') + ... + ISNULL(TN.Field,'')

答案 1 :(得分:3)

终于找到了......我之前没有意识到这是可能的:

select

  case when T1.Field is not null then 'T1,' else '' end
+ case when T2.Field is not null then 'T2,' else '' end
+ case when T3.Field is not null then 'T3,' else '' end as result

from T1 
left outer join T2 on ...
left outer join T3 on ...