希望多次根据相同的分隔符在一列中拆分字符串,并将每个字符串数组输入到同一个表中的单独的表列中,例如。
DECLARE @Test VARCHAR(50)
SET @Test = '0^5^6^104^0^0^5'
预期输出
column1 column2 column3 column4 column5 column6 column7
0 5 6 104 0 0 5
答案 0 :(得分:0)
DECLARE @Test table (val VARCHAR(50))
insert into @Test (val)values ('0^5^6^104^0^0^5')
;WITH CTE AS (
SELECT
Split.a.value('.', 'VARCHAR(100)') AS String
FROM (SELECT
CAST ('<M>' + REPLACE([val], '^', '</M><M>') + '</M>' AS XML) AS String
FROM @Test) AS A CROSS APPLY String.nodes ('/M') AS Split(a))
Select [1]Col1,[6]Col2,[7]Col3,[4]Col1,[2]Col1,[3]Col1,[5]Col1 from (
select T.RN,T.String from (
select Row_number()OVER( ORDER BY string )RN,string from CTE)T)K
PIVOT(MAX(string) FOR RN IN ([1],[2],[3],[4],[5],[6],[7]))P
答案 1 :(得分:0)
DECLARE @Test VARCHAR(50)
SET @Test = '0^5^6^104^0^0^5'
select * from (
select t.col.value('.','varchar(max)') as val, 'column' + cast(ROW_NUMBER() over (order by (select 1)) as varchar(10)) as rn from
(
select cast('<m>' + REPLACE(@test,'^','</m><m>') + '</m>' as xml) as val
) as abc
cross apply abc.val.nodes('/m') as t(col)
) as t1
pivot (max(val) for rn in (column1,column2,column3,column4,column5,column6,column7)) as pvt
答案 2 :(得分:-1)
试试这个
DECLARE @Test VARCHAR(50) SET @Test = '0^5^6^104^0^0^5' SET @Test = N'SELECT N''' + REPLACE(@Test,'^',''' ,''') + '''' exec(@Test)