![](/img/trans.png)
[英]Concatenate column names in sql select statement if certain condition is met
[英]Concatenate multiple column names into 1 column SQL
我在这里遇到小问题。 我有一张四列的桌子
Column1 | Column2 | Column3 | Column4
NULL.........NULL.........NULL.........NULL
ABC..........NULL..........XYZ...........NULL
列1,2和3可能有一些值或为空,列4为空
当column1,2或3中的任何一个都不为null时,我必须使用该列名更新column4。 我期待以下结果
Column1 | Column2 | Column3 | Column4
NULL.........NULL.........NULL.........NULL
ABC..........NULL..........XYZ...........Column1,Column3
谁能指导我如何实现这一目标。
任何帮助表示赞赏。 提前致谢。
使用一种情况来测试空值,并在其不为空时返回列名。 那里的东西是删除您在字符串中第一个得到的多余逗号。
update YourTable
set Column4 = stuff(case when Column1 is not null then ',Column1' else '' end +
case when Column2 is not null then ',Column2' else '' end +
case when Column3 is not null then ',Column3' else '' end, 1, 1, '')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.