Using this bit of code:
DECLARE @t TABLE( ID INT IDENTITY, data VARCHAR(50), data2 VARCHAR(50))
INSERT INTO @t(data,data2) SELECT 'AA,AB,AC,AD', 'BA,BB,BC,BD'
SELECT F1.id,
F1.data,
f1.data2,
O.splitdata
FROM
(SELECT *,cast('<X>'+replace(F.data,',','</X><X>')+'</X>' as XML) as xmlfilter from @t F)F1
CROSS APPLY
(SELECT fdata.D.value('.','varchar(50)') as splitdata FROM f1.xmlfilter.nodes('X') as fdata(D)) O
We get these results:
id data data2 splitdata
1 AA,AB,AC,AD BA,BB,BC,BD AA
1 AA,AB,AC,AD BA,BB,BC,BD AB
1 AA,AB,AC,AD BA,BB,BC,BD AC
1 AA,AB,AC,AD BA,BB,BC,BD AD
But what I really want is to match the 2 columns "data" and "data2" together, to get this:
id data data2 splitdata split2
1 AA,AB,AC,AD BA,BB,BC,BD AA BA
1 AA,AB,AC,AD BA,BB,BC,BD AB BB
1 AA,AB,AC,AD BA,BB,BC,BD AC BC
1 AA,AB,AC,AD BA,BB,BC,BD AD BD
i.e. a match of the 2 csv columns across each element number.
I've been trying all sorts of XML combinations and using ROW_NUMBER() to get an original order number of the csv elements, but can't get it working.
Any help gratefully received.