Previously someone asked about Turning a Comma Separated string into individual rows. This works great for one column, but I have an extension question. What if I want to split two (or more) related columns. That is, if I have data like this (using the data from the previous post as a base):
| SomeID | OtherID | Data | RelatedData |
+----------------+-------------+-----------+-------------+
| abcdef-..... | cdef123-... | 18,20,22 | xxx,,yyy |
| abcdef-..... | 4554a24-... | 17,19 | a,bb |
| 987654-..... | 12324a2-... | 13,19,20 | r,s,t |
And want to return rows like the following:
| SomeID | OtherID | Data | RelatedData |
+----------------+-------------+------+-------------+
| abcdef-..... | cdef123-... | 18 | xxx |
| abcdef-..... | cdef123-... | 20 | |
| abcdef-..... | cdef123-... | 22 | yyy |
| abcdef-..... | 4554a24-... | 17 | a |
| abcdef-..... | 4554a24-... | 19 | bb |
| ... | ... | ... | ... |
I was hoping to use STRING_SPLIT due to its simplicity, but I cannot find a way to make it work. This does not work:
select OtherID, cs1.Value, cs2.Value
from yourtable
cross apply STRING_SPLIT (Data, ',') cs1
cross apply STRING_SPLIT (RelatedData, ',') cs2
Any suggestions?