Using SQL Server 2016.
I have the following table
[ID] [Name List1] [Name List2] [Name List3]
1 a1,a2,a3 a21,a22 a31,a32,a33,a34,a45
2 b1,b2 b21,b22,b23,b24 b31
3 etc....
[ID] is a unique identifier column.
I need to split all these comma delimited fields into separate records.
What I did so far:
SELECT a.*, b.[Name List1] FROM [TABLE1] a LEFT JOIN
(SELECT DISTINCT [ID], value AS [Name List1]
FROM [TABLE1] CROSS APPLY STRING_SPLIT([Name List1], ',')
WHERE value IS NOT NULL AND rtrim(value) <> '') b ON a.[ID]=b.[ID]
This query will split records based on the first column, i.e. [Name List1] but I need to do it for all columns ([Name List2] and [Name List3] as well).
Is there an elegant way to achieve it with minimum coding?
Desired result should include all possible combinations of these comma delimited values:
[ID] [Name List1] [Name List2] [Name List3]
1 a1 a21 a31
2 a2 a21 a31
3 a3 a21 a31
4 etc... meaning all possible combination of column splits
id = 1and replicate that for allids. That doesn't match the rest of the question. Please take the effort and post an actual result, that matches the posted sample data.all possible combination of column splits- that is simply threecross applyin a row.[TABLE1] CROSS APPLY STRING_SPLIT([Name List1], ',') l1 CROSS APPLY STRING_SPLIT([Name List2], ',') l2 CROSS APPLY STRING_SPLIT([Name List3], ',') l3.