将多个逗号分隔的列分割为行

时间:2022-04-23 21:38:13

I have one table (SQL Server), which has comma separated values in multiple columns, like below:

我有一个表(SQL Server),它在多个列中有逗号分隔的值,如下所示:

Rule_ID    ListType_ID    Values
1          1,2            100,200
2          3,4            300,400

I want to split the comma separated values and convert them into rows.

我想分割逗号分隔的值并将它们转换为行。

The required output must be like below:

所需的输出必须如下所示:

Rule_ID    ListType_ID    Values
1          1              100
1          2              200
2          3              300
2          4              400

I have tried the below query:

我已经尝试了以下查询:

DECLARE @TEMP AS TABLE (
    [Rule_ID] INT,
    [ListType_ID] VARCHAR(MAX),
    [Values] VARCHAR(MAX)
)

INSERT INTO @TEMP
SELECT 1, '1,2', '100,200'
UNION ALL
SELECT 2, '3,4', '300,400' 

SELECT 
    [Rule_ID],
    PARSENAME(REPLACE(Split1.b.value('.', 'VARCHAR(100)'),'-','.'),1) AS [ListType_ID],
    PARSENAME(REPLACE(Split.a.value('.', 'VARCHAR(100)'),'-','.'),1) AS [Values] 
FROM  
(
    SELECT [Rule_ID],
    CAST ('<M>' + REPLACE([ListType_ID], ',', '</M><M>') + '</M>' AS XML) AS [ListType_ID],
    CAST ('<M>' + REPLACE([Values], ',', '</M><M>') + '</M>' AS XML) AS [Values] 
    FROM @TEMP     
) AS A 
CROSS APPLY [Values].nodes ('/M') AS Split(a)
CROSS APPLY [ListType_ID].nodes ('/M') AS Split1(b)
ORDER BY [Rule_ID], [ListType_ID], [Values]

This query returns the below output, which is different from the required output:

该查询返回低于所需输出的输出:

Rule_ID    ListType_ID    Values
1          1              100
1          1              200
1          2              100
1          2              200
2          3              300
2          3              400
2          4              300
2          4              400

Please help me here....!!!!

请帮我在这里.... ! ! ! !

2 个解决方案

#1


3  

Please check following SQL script

请检查下面的SQL脚本

To split string in SQL I used one of the following user-defined SQL split string functions

为了在SQL中分割字符串,我使用了以下用户定义的SQL分割字符串函数之一

These functions return the order of the splitted string which I used in WHERE clause so I can map field values one-to-one

这些函数返回我在WHERE子句中使用的分割字符串的顺序,因此我可以映射字段值一一对应。

/*
create table Table_1 (
    Rule_ID int,    ListType_ID    varchar(max), [Values] varchar(max)
)
insert into Table_1 select 1,'1,2','100,200'
insert into Table_1 select 2,'3,4','300,400'
*/
select 
Rule_ID,
idlist.val as ListType_ID,
valueslist.val as [Values]
from Table_1
cross apply dbo.SPLIT(ListType_ID,',') as idlist
cross apply dbo.SPLIT([Values],',') as valueslist
where 
idlist.id = valueslist.id

#2


1  

Using CTE, a double CROSS APPLY and XML based split you can use this script:

使用CTE、双交叉应用和基于XML的分割,您可以使用以下脚本:

;WITH Splitted AS 
( 
    SELECT    
        [Rule_ID]
        ,CAST('<x>' + REPLACE([ListType_ID],',','</x><x>') + '</x>' AS XML) AS  [ListType_ID_Val]    
        ,CAST('<x>' + REPLACE([Values],',','</x><x>') + '</x>' AS XML) AS  [Values_Val]    
    FROM @TEMP 
) 
SELECT     
    Rule_ID, cs.VAL as[ListType_ID], cd.VAL as [Values]
FROM Splitted
    CROSS APPLY (VALUES ('a',ListType_ID_Val.value(N'/x[1]','int') ),
      ('b',ListType_ID_Val.value(N'/x[2]','int') )
    )CS (COL,VAL) 
     CROSS APPLY (VALUES ('a',Values_Val.value(N'/x[1]','int') ),
      ('b',Values_Val.value(N'/x[2]','int') )
    )CD (COL,VAL) 
where CS.COL = CD.COL

Results:

结果:

将多个逗号分隔的列分割为行

#1


3  

Please check following SQL script

请检查下面的SQL脚本

To split string in SQL I used one of the following user-defined SQL split string functions

为了在SQL中分割字符串,我使用了以下用户定义的SQL分割字符串函数之一

These functions return the order of the splitted string which I used in WHERE clause so I can map field values one-to-one

这些函数返回我在WHERE子句中使用的分割字符串的顺序,因此我可以映射字段值一一对应。

/*
create table Table_1 (
    Rule_ID int,    ListType_ID    varchar(max), [Values] varchar(max)
)
insert into Table_1 select 1,'1,2','100,200'
insert into Table_1 select 2,'3,4','300,400'
*/
select 
Rule_ID,
idlist.val as ListType_ID,
valueslist.val as [Values]
from Table_1
cross apply dbo.SPLIT(ListType_ID,',') as idlist
cross apply dbo.SPLIT([Values],',') as valueslist
where 
idlist.id = valueslist.id

#2


1  

Using CTE, a double CROSS APPLY and XML based split you can use this script:

使用CTE、双交叉应用和基于XML的分割,您可以使用以下脚本:

;WITH Splitted AS 
( 
    SELECT    
        [Rule_ID]
        ,CAST('<x>' + REPLACE([ListType_ID],',','</x><x>') + '</x>' AS XML) AS  [ListType_ID_Val]    
        ,CAST('<x>' + REPLACE([Values],',','</x><x>') + '</x>' AS XML) AS  [Values_Val]    
    FROM @TEMP 
) 
SELECT     
    Rule_ID, cs.VAL as[ListType_ID], cd.VAL as [Values]
FROM Splitted
    CROSS APPLY (VALUES ('a',ListType_ID_Val.value(N'/x[1]','int') ),
      ('b',ListType_ID_Val.value(N'/x[2]','int') )
    )CS (COL,VAL) 
     CROSS APPLY (VALUES ('a',Values_Val.value(N'/x[1]','int') ),
      ('b',Values_Val.value(N'/x[2]','int') )
    )CD (COL,VAL) 
where CS.COL = CD.COL

Results:

结果:

将多个逗号分隔的列分割为行