如何使用SQL合并拆分数据

时间:2015-01-25 19:28:43

标签: sql sql-server tsql merge sql-server-2012

原始答案:
我需要按指定的分隔符拆分字符串。我使用Numbers Table使用SQL Performance - T-SQL Queries - Split Strings上的指南实现了此目的。但是,一旦字符串被拆分,如何将其合并回列中?所以这种形式的分割字符串的结果是:

**Item**
a
id
xyz
abcd
a
id
xyz
abcd

转换为此表单:

C1 C2  C3   C4
a  id  xyz  abc
a  id  xyz  abc

等等?


根据Goat的回答更新

/abc/def/ghi/jkl.html_abc_def

应该分成

C1   C2   C3   C4   C5
abc  def  ghi  jkl  html_abc_def

这是我到目前为止所拥有的

WITH CTE AS (SELECT testString = 'abc/def/ghi/jkl.html_abc_def'
            ),
     splitString
     AS (SELECT *,
         CONVERT(XML, '<String><Section>'
                      + Replace(Replace((substring(teststring,1,patindex('%.html%',teststring)-1)),'_','/'), '/', '</Section><Section>')
                      + '</Section></String>') AS xmlString
         FROM   CTE)
SELECT xmlstring.value('/String[1]/Section[1]', 'varchar(100)') AS Col1,
       xmlstring.value('/String[1]/Section[2]', 'varchar(100)') AS Col2,
       xmlstring.value('/String[1]/Section[3]', 'varchar(100)') AS Col3,
       xmlstring.value('/String[1]/Section[4]', 'varchar(100)') AS Col4,
       xmlstring.value('/String[1]/Section[5]', 'varchar(100)') AS Col5,
       xmlstring.value('/String[1]/Section[6]', 'varchar(100)') AS Col6
FROM   splitstring;

--select substring(teststring,1,patindex('%.html%',teststring)-1) first
--substring(teststring,patindex('%.html%',teststring)+1,200) second

第一个子字符串将数据获取到.html,第二个子字符串获取数据。但我不知道怎么把它们放在一起。

明白了:

WITH CTE AS (SELECT testString = 'abc/def/ghi/jkl.html_abc_def'
            ),
     splitString
     AS (SELECT *,
         CONVERT(XML, '<String><Section>'
                      + Replace(Replace((substring(teststring,1,patindex('%.html%',teststring)-1)),'_','/'), '/', '</Section><Section>')
                      + '</Section></String>'
                      + '<String><Section>'
                      + substring(teststring,patindex('%.html%',teststring)+1,200)
                      + '</Section></String>'
                      ) AS xmlString
         FROM   CTE)
SELECT xmlstring.value('/String[1]/Section[1]', 'varchar(100)') AS Col1,
       xmlstring.value('/String[1]/Section[2]', 'varchar(100)') AS Col2,
       xmlstring.value('/String[1]/Section[3]', 'varchar(100)') AS Col3,
       xmlstring.value('/String[1]/Section[4]', 'varchar(100)') AS Col4,
       xmlstring.value('/String[2]/Section[1]', 'varchar(100)') AS Col5
FROM   splitstring;

1 个答案:

答案 0 :(得分:0)

您可以使用XML功能将分隔的字符串拆分为各个列,而不是先拆分为行:

;WITH cte AS (SELECT testString = 'test,1,2,3'
              UNION  SELECT 'test,2,3,4'
              UNION  SELECT 'test,5,6,7')
     ,SplitString AS (SELECT *,
                             CONVERT(XML,'<String><Section>'+ REPLACE(testString,',', '</Section><Section>') + '</Section></String>') AS xmlString
                      FROM cte
                      )    
 SELECT xmlString.value('/String[1]/Section[1]','varchar(100)') AS Col1
       ,xmlString.value('/String[1]/Section[2]','varchar(100)') AS Col2
       ,xmlString.value('/String[1]/Section[3]','varchar(100)') AS Col3
       ,xmlString.value('/String[1]/Section[4]','varchar(100)') AS Col4
 FROM SplitString 

注意:上面使用逗号作为分隔符,相应地更改REPLACE()

更新:很高兴您对它进行了排序,看起来您可能会过度复杂化XML创建,我认为您可以将.替换为/然后在第二个/中使用REPLACE()

;WITH cte AS (SELECT testString = 'abc/def/ghi/jkl.html_abc_def')
     ,SplitString AS (SELECT *,
                             CONVERT(XML,'<String><Section>'+ REPLACE(REPLACE(testString,'.','/'),'/', '</Section><Section>') + '</Section></String>') AS xmlString
                      FROM cte
                      )    
 SELECT xmlString.value('/String[1]/Section[1]','varchar(100)') AS Col1
       ,xmlString.value('/String[1]/Section[2]','varchar(100)') AS Col2
       ,xmlString.value('/String[1]/Section[3]','varchar(100)') AS Col3
       ,xmlString.value('/String[1]/Section[4]','varchar(100)') AS Col4
       ,xmlString.value('/String[1]/Section[5]','varchar(100)') AS Col5
 FROM SplitString