declare @T table(ID int, SN varchar(100), Types varchar(1000))
insert into @T
select 1, 123, 'ABC,XYZ,TEST' union all
select 2, 234, 'RJK,CDF,TTT,UMB,UVX' union all
select 4, 234, 'XXX' union all
select 3, 345, 'OID,XYZ'
;with cte(ID, SN, Types, Rest) as
(
select ID,
SN,
cast(substring(Types+',', 1, charindex(',', Types+',')-1) as varchar(100)),
stuff(Types, 1, charindex(',', Types), '')+','
from @T
where len(Types) > 0
union all
select ID,
SN,
cast(substring(Rest, 1, charindex(',', Rest)-1) as varchar(100)),
stuff(Rest, 1, charindex(',', Rest), '')
from cte
where len(Rest) > 0
)
select ID, SN, Types
from cte
order by ID
I use a recursive CTE to split the string. The third column Types
is populated with the first word in the Types column of @T. Stuff
will then remove the first word and populate the Rest column that then will contain everything but the first word. After UNION ALL is the recursive part that basically do the exact same thing but it uses the CTE as a source and it uses the rest
column to pick the first word. The first word of the rest
column is removed with stuff
and then ..... well it is recursive so I think I will stop here with the explanation. The recursive part will end when there are no more words left
where len(Rest) > 0
.