これを試してみてください -
クエリ:
DECLARE @table TABLE
(
a VARCHAR(200)
, b VARCHAR(200)
)
INSERT INTO @table(a, b)
VALUES
('1,2,3,', 'abc, def, ghi,'),
('1,2,3,4,', 'test1, test2, test3, test4,')
SELECT
[Item name] = LTRIM(data.p)
, [Item type] = data.r
FROM (
SELECT
p = p.value('(.)[1]', 'NVARCHAR(50)')
, po = p.value('for $i in . return count(../*[. << $i])', 'int')
, r = r.value('(.)[1]', 'NVARCHAR(50)')
, ro = r.value('for $i in . return count(../*[. << $i])', 'int')
FROM (
SELECT
txml = CAST('<r><s>' + REPLACE(SUBSTRING(t.a, 1, LEN(t.a) - 1), ',', '</s>' + '<s>') + '</s></r>' AS XML)
, kxml = CAST('<r><s>' + REPLACE(SUBSTRING(t.b, 1, LEN(t.b) - 1), ',', '</s>' + '<s>') + '</s></r>' AS XML)
FROM @table t
) d
CROSS APPLY kxml.nodes('/r/s') t(p)
CROSS APPLY txml.nodes('/r/s') k(r)
) data
WHERE data.po = data.ro
出力:
Item name Item type
----------- ----------
abc 1
def 2
ghi 3
test1 1
test2 2
test3 3
test4 4