在SQL Server中使用分隔符来连接列值

时间:2019-01-10 04:55:26

标签: sql-server

我想用分隔符连接列值并将其分配给变量。

如果列值为null,则无需添加分隔符。

例如:A|B|C|D

如果B为空A|C|D

我尝试使用CONCAT函数,但是如果B为空,则结果为A||C|D

DECLARE @OldValue VARCHAR(8000); 

SELECT @OldValue =  CONCAT([FloorCode],'|',
                           [FloorName],'|',
                           [BuildingID],'|',
                           [HCMLocationCode],'|',
                           [IsActive]) 
FROM tblFloor_Master 
WHERE  FloorID = @FloorID; 

@FloorID是SP的输入参数

4 个答案:

答案 0 :(得分:0)

您可以尝试以下查询。

create table tempTable (id int identity(1, 1),col1 char(1), col2 char(1), col3 char(1), col4 char(1))
insert into tempTable values ('A', NULL, 'C', 'D')

select * into #NewTable from(
select id, col1 as Value from tempTable where col1 is not null
union
select id, col2 as Value from tempTable where col2 is not null
union
select id, col3 as Value from tempTable where col3 is not null
union
select id, col4 as Value from tempTable where col4 is not null
)a

SELECT  ID
       ,STUFF((SELECT '| ' + CAST(Value AS VARCHAR(10)) [text()]
         FROM #NewTable 
         WHERE ID = t.ID
         FOR XML PATH(''), TYPE)
        .value('.','NVARCHAR(MAX)'),1,1,' ') List_Output
FROM #NewTable t
GROUP BY ID

输出如下所示

ID  List_Output
---------------
1     A| C| D

如果您不想在值之间放置空格,则可以尝试

SELECT  ID
       ,STUFF((SELECT '|' + CAST(Value AS VARCHAR(10)) [text()]
         FROM #NewTable 
         WHERE ID = t.ID
         FOR XML PATH(''), TYPE)
        .value('.','NVARCHAR(MAX)'),1,1,'') List_Output
FROM #NewTable t
GROUP BY ID

在这种情况下,输出将是

ID  List_Output
---------------
1   A|C|D

您也可以使用存储过程尝试以下实际查询

create table tblFloor_Master (FloorID int identity(1, 1),
                              FloorCode char(1), 
                              FloorName char(1),
                              BuildingID char(1),
                              HCMLocationCode char(1))

insert into tblFloor_Master values ('A', NULL, 'C', 'D')

GO
--To create a procedure
create proc uspGetConcateValue
@FloorId int
as
BEGIN
    select * into #tblFloor_Master from(
    select FloorId, FloorCode as Value from tblFloor_Master where FloorCode is not null
    union
    select FloorId, FloorName as Value from tblFloor_Master where FloorName is not null
    union
    select FloorId, BuildingID as Value from tblFloor_Master where BuildingID is not null
    union
    select FloorId, HCMLocationCode as Value from tblFloor_Master where HCMLocationCode is not null
    )a

    SELECT  FloorId
           ,STUFF((SELECT '|' + CAST(Value AS VARCHAR(10)) [text()]
             FROM #tblFloor_Master 
             WHERE FloorId = t.FloorId
             FOR XML PATH(''), TYPE)
            .value('.','NVARCHAR(MAX)'),1,1,'') List_Output
    FROM #tblFloor_Master t
    where FloorID = @FloorId
    GROUP BY FloorId
END

此处为现场演示-Live Demo

答案 1 :(得分:0)

SELECT @OldValue = CONCAT('',
    CASE WHEN [FloorCode] IS NULL THEN '' ELSE CONCAT([FloorCode],'|') END,
    CASE WHEN [FloorName] IS NULL THEN '' ELSE CONCAT([FloorName],'|') END,
    CASE WHEN [BuildingID] IS NULL THEN '' ELSE CONCAT([BuildingID],'|') END,
    CASE WHEN [HCMLocationCode] IS NULL THEN '' ELSE CONCAT([HCMLocationCode],'|') END,
    [IsActive])
FROM tblFloor_Master 
WHERE  FloorID = @FloorID; 

答案 2 :(得分:0)

选择    TRIM('|'FROM       regexp_replace(           CONCAT('','|','COLB','|','','|','COLD','|','COLE')           ,'[\ |] +'           ,'|'           ,'G'       )    )

答案 3 :(得分:0)

应该像做replace一样简单,替换||。到|然后替换|||到|由于最大数量|是3。

DECLARE @OldValue VARCHAR(8000); 

SELECT @OldValue =  replace(replace(CONCAT([FloorCode],'|',
                           [FloorName],'|',
                           [BuildingID],'|',
                           [HCMLocationCode],'|',
                           [IsActive]),'||','|'),'|||','|')
FROM tblFloor_Master 
WHERE  FloorID = @FloorID;