将单个分隔字段展开为多个列/行

时间:2015-09-04 10:57:37

标签: sql sql-server

我在名为Body的SQL表中有一个文本字段,其中包含多行分隔文本(例如,请参阅下面的数据)。我正在寻找SQL查询的帮助,该查询将,分隔数据拆分为多行和列到我可以在视图中引用的临时表。

74,232,3,2700,2619,524,C,0,0,1,47,90,537,9355,0,PA,1,1,SCO1DS,,0,0
74,232,6,5400,5238,1048,C,0,0,1,47,81,546,6362,0,PB,1,1,INT3DS,,0,0
74,232,6,837,810,162,C,0,0,75,55,121,100,N694297,0,PC,6,1,GLA2,,0,0
74,232,8,884,857,171,C,0,0,2,45,90,766,6492,0,PW,8,1,WOL1DS,,0,0

2 个答案:

答案 0 :(得分:1)

我只使用了10个你可以根据需要使用的字段。

测试数据

DECLARE @t TABLE (Body VARCHAR(MAX))
INSERT INTO @t VALUES 
('74,232,3,2700,2619,524,C,0,0,1,47,90,537,9355,0,PA,1,1,SCO1DS,,0,0'),
('74,232,6,5400,5238,1048,C,0,0,1,47,81,546,6362,0,PB,1,1,INT3DS,,0,0'),
('74,232,6,837,810,162,C,0,0,75,55,121,100,N694297,0,PC,6,1,GLA2,,0,0'),
('74,232,8,884,857,171,C,0,0,2,45,90,766,6492,0,PW,8,1,WOL1DS,,0,0');

查询

WITH Split_Fields (ID,xmlfields)
AS
(
    SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) ID
    ,CONVERT(XML,'<Fields><field>'  
    + REPLACE(Body,',', '</field><field>') + '</field></Fields>') AS xmlfields
      FROM @t
)

 SELECT ID      
 ,xmlfields.value('/Fields[1]/field[1]','varchar(100)') AS Field1    
 ,xmlfields.value('/Fields[1]/field[2]','varchar(100)') AS Field2
 ,xmlfields.value('/Fields[1]/field[3]','varchar(100)') AS Field3    
 ,xmlfields.value('/Fields[1]/field[4]','varchar(100)') AS Field4
 ,xmlfields.value('/Fields[1]/field[5]','varchar(100)') AS Field5
 ,xmlfields.value('/Fields[1]/field[6]','varchar(100)') AS Field6    
 ,xmlfields.value('/Fields[1]/field[7]','varchar(100)') AS Field7
 ,xmlfields.value('/Fields[1]/field[8]','varchar(100)') AS Field8    
 ,xmlfields.value('/Fields[1]/field[9]','varchar(100)') AS Field9
 ,xmlfields.value('/Fields[1]/field[10]','varchar(100)') AS Field10
 FROM Split_Fields

结果

╔════╦════════╦════════╦════════╦════════╦════════╦════════╦════════╦════════╦════════╦═════════╗
║ ID ║ Field1 ║ Field2 ║ Field3 ║ Field4 ║ Field5 ║ Field6 ║ Field7 ║ Field8 ║ Field9 ║ Field10 ║
╠════╬════════╬════════╬════════╬════════╬════════╬════════╬════════╬════════╬════════╬═════════╣
║  1 ║     74 ║    232 ║      3 ║   2700 ║   2619 ║    524 ║ C      ║      0 ║      0 ║       1 ║
║  2 ║     74 ║    232 ║      6 ║   5400 ║   5238 ║   1048 ║ C      ║      0 ║      0 ║       1 ║
║  3 ║     74 ║    232 ║      6 ║    837 ║    810 ║    162 ║ C      ║      0 ║      0 ║      75 ║
║  4 ║     74 ║    232 ║      8 ║    884 ║    857 ║    171 ║ C      ║      0 ║      0 ║       2 ║
╚════╩════════╩════════╩════════╩════════╩════════╩════════╩════════╩════════╩════════╩═════════╝

答案 1 :(得分:0)

Aaron Betrand详细介绍了如何做类似here的事情。

最佳表现来自CLR。

CREATE ASSEMBLY CLRUtilities FROM 'c:\DLLs\CLRUtilities.dll' 
  WITH PERMISSION_SET = SAFE;
GO

CREATE FUNCTION dbo.SplitStrings_CLR
(
   @List      NVARCHAR(MAX),
   @Delimiter NVARCHAR(255)
)
RETURNS TABLE ( Item NVARCHAR(4000) )
EXTERNAL NAME CLRUtilities.UserDefinedFunctions.SplitString_Multi;
GO

通过使用他的一个示例,您可以扩展它以将字符串写入临时表。