Skip to main content

Converting a single comma separated row into multiple rows

we can have it like this 

 

SELECT A.[State], Split.a.value('.', 'VARCHAR(100)') AS String FROM (SELECT [State], CAST ('<M>' + REPLACE([City], ',', '</M><M>') + '</M>' AS XML) AS String FROM TableA) AS A CROSS APPLY String.nodes ('/M') AS Split(a);

 

assuiming we have table with two columns

1- State.

2- City (separated by Comma)

Tags