连接多个列,具有空值

时间:2018-01-18 09:55:01

标签: sql sql-server-2008 concatenation

我希望在MS-SQL Server 2008中连接多个以;分隔的列。

我遇到的问题是,如果没有CONCAT(),我不知道如何处理NULL列,而不会产生这样的结果tattoos;comics;;;

您可以在此处创建示例数据的脚本:

declare @tbl as table (
    id int
    ,kw1 varchar(15)
    ,kw2 varchar(15)
    ,kw3 varchar(15)
    ,kw4 varchar(15)
    ,kw5 varchar(15)
);

insert into @tbl values
(1, 'innocence', 'graphic novel', 'cartoon', NULL, 'comics')
,(2, 'tattoos', 'comics', NULL, NULL, NULL)
,(3, NULL, 'music', 'cartoon', 'adventure', 'film') 

表格:

+----+-----------+---------------+---------+-----------+--------+
| id |    kw1    |      kw2      |   kw3   |    kw4    |  kw5   |
+----+-----------+---------------+---------+-----------+--------+
|  1 | innocence | graphic novel | cartoon | NULL      | comics |
|  2 | tattoos   | comics        | NULL    | NULL      | NULL   |
|  3 | NULL      | music         | cartoon | adventure | film   |
+----+-----------+---------------+---------+-----------+--------+

所以我的实际结果如下:

+----+-----------------------------------------+
| id |                Keywords                 |
+----+-----------------------------------------+
|  1 | innocence;graphic novel;cartoon;;comics |
|  2 | tattoos;comics;;;                       |
|  3 | ;music;cartoon;adventure;film           |
+----+-----------------------------------------+

但这就是我想要的:

+----+----------------------------------------+
| id |                Keywords                |
+----+----------------------------------------+
|  1 | innocence;graphic novel;cartoon;comics |
|  2 | tattoos;comics                         |
|  3 | music;cartoon;adventure;film           |
+----+----------------------------------------+

查询:

SET CONCAT_NULL_YIELDS_NULL OFF;

select
    id
    ,kw1 + ';' + kw2 + ';' + kw3 + ';' + kw4 + ';' + kw5 as Keywords
FROM @tbl

感谢任何帮助!

3 个答案:

答案 0 :(得分:2)

如果没有CONCAT()(SQL Server 2008+),您可以使用ISNULL(),如下所示:

SELECT 
    t.id ,
    ISNULL(t.kw1 + ';', '') + ISNULL(t.kw2 + ';', '') +
    ISNULL(t.kw3 + ';', '') + ISNULL(t.kw4 + ';', '') +
    ISNULL(t.kw5 + ';', '') AS Vals
FROM
    @tbl AS t;

如果列值为NULL,则加入NULL + ';'会生成NULL,因此会为您提供空字符串''

对于2008+,您可以像CONCAT()那样使用:

SELECT 
    t.id ,
    CONCAT(t.kw1 + ';' ,t.kw2 + ';',t.kw3 + ';' ,t.kw4 + ';', t.kw5+ ';') as Vals
FROM 
    @tbl AS t

两者都产生了这个结果:

id          Vals
----------- -----------------------------------------------
1           innocence;graphic novel;cartoon;comics;
2           tattoos;comics;
3           music;cartoon;adventure;film;

答案 1 :(得分:0)

目前,我只找到了使用CASE准确输出的方法,以便您可以尝试使用它:

SELECT
id,
keywords = (
CASE WHEN kw1 IS NOT NULL THEN kw1 + CASE WHEN COALESCE(kw2,kw3,kw4,kw5) IS NOT NULL THEN ';' ELSE '' END ELSE '' END +
CASE WHEN kw2 IS NOT NULL THEN kw2 + CASE WHEN COALESCE(kw3,kw4,kw5) IS NOT NULL THEN ';' ELSE '' END ELSE '' END +
CASE WHEN kw3 IS NOT NULL THEN kw3 + CASE WHEN COALESCE(kw4,kw5) IS NOT NULL THEN ';' ELSE '' END ELSE '' END +
CASE WHEN kw4 IS NOT NULL THEN kw4 + CASE WHEN kw5 IS NOT NULL THEN ';' ELSE '' END ELSE '' END +
CASE WHEN kw5 IS NOT NULL THEN kw5 ELSE '' END)
FROM @tbl

<强>输出:

id  keywords
-----------------------------------------
1   innocence;graphic novel;cartoon;comics
2   tattoos;comics
3   music;cartoon;adventure;film

答案 2 :(得分:-1)

你可以试试这个:

     select ISNULL(kwa1,'') + ';' + ISNULL(kw2,'') from TABLENAME