Thursday, October 15, 2015

Create multiple records for delimited values in single column



declare @Tab Table ( Col nvarchar(100), type nvarchar(100))

Insert into @Tab values ('abc','a,b,c,d')
Insert into @Tab values ('xyz','x,y,z')
Insert into @Tab values ('pqr','p,')
Insert into @Tab values ('rst',',')
Insert into @Tab values ('lmn',' ')
Insert into @Tab values ('opq','o,p,q,r,t,y,u,i,o,p,d')



Select *--Col, ltrim(rtrim(split_value))
From (
    Select *,  Cast('~x~'+Replace(d.type,',','~/x~~x~')+'~/x~' As XML) As types
    From @Tab d
) x
Cross Apply (
    Select ttsd.x.value('.', 'varchar(255)') as split_value
    From x.types.nodes('x') as ttsd(x)

) c

No comments:

Post a Comment