Search This Blog

Sunday, February 6, 2011

Spilt comma separated values into separeted rows



Actutal table ::
   


Step_Configuration_Idstep_modes
1,7202,
2,7202,7204,7201,7203,7200,
3,7202,
5,7201,7203,7200,
6,7202,7201,7203,7200,



Excepted output::


Step_Configuration_Idstep_nodes
17202
27204
27200
27202
27201
27203
37202
57201
57200
57203
67202
67200
67201
67203


Solution:

select
distinct Step_Configuration_Id,i.value('.', 'VARCHAR(20)') AS step_modes
from
(select Step_Configuration_Id,step_modes ,CAST('<i>'+ replace(step_modes,',','</i><i>')
+
'</i>' AS XML) AS step_modesXml from Step_Configurations
)
a CROSS APPLY step_modesXml.nodes('i') x(i) where i.value('.', 'VARCHAR(20)') <> ''
order by 1
 

No comments:

Post a Comment