I inherited a table (who hasn't, right?) that has data that looks like this:
Item | Properties | Quantity
--------------------------------------------------------------------
Shirt | button-down,polo,sleeveless | 4,5,8
For the short term, I want to create a view, but eventually I want to export the data to a new version of the table when time allows and have it more like:
Item | Properties | Quantity
--------------------------------------------------------------------
Shirt | button-down | 4
Shirt | polo | 5
Shirt | sleeveless | 8
Essentially, take multiple column groups (I imagine there will be other tables where there are more columns than two with this sort of behavior) that are known to be delimiter separated and break them into distinct rows? Any other rows gathered that aren't like this would be shared amongst them like Item in this example. # of commas are uniform between these types.
edit: I used the function given in the answer to How to convert comma separated NVARCHAR to table records in SQL Server 2005? and this is currently what I have:
select distinct data.item, tmptbl.[String] from
data cross apply [ufn_CSVToTable](data.properties, ',') tmptbl ...
This works in the single column context, but applying that function to a second column (quantity in this case) outright would generate every possible combination of properties and quantities, right? In fact, yes it did result in that when I attempted. It would seem I'd need a cursor or similar to effectively break into individual rows of properties[i] | quantity[i], will try and construct that. That or may just select the data over and split it on the application side.