Table
x-----------------x--------------------x | ID | INTERESTS | x-----------------x--------------------x | 000CT00002UA | Golf,food | | 000CT12303CB | Cricket,Bat | x------x----------x--------------------x
METHOD 1: Using the XML Format
SELECT ID,Split.a.value('.', 'VARCHAR(100)') 'INTERESTS' FROM ( -- To change ',' to any other delimeter, just change ',' before '</M><M>' to your desired one SELECT ID, CAST ('<M>' + REPLACE(INTERESTS, ',', '</M><M>') + '</M>' AS XML) AS Data FROM TEMP ) AS A CROSS APPLY Data.nodes ('/M') AS Split(a)
METHOD 2: Using the dbo.Split
SELECT a.ID, b.items FROM
Here's the dbo.Split function.
CREATE FUNCTION [dbo].[Split](@String varchar(8000), @Delimiter char(1)) returns @temptable TABLE (items varchar(8000)) as begin declare @idx int declare @slice varchar(8000) select @idx = 1 if len(@String)<1 or @String is null return while @idx!= 0 begin set @idx = charindex(@Delimiter,@String) if @idx!=0 set @slice = left(@String,@idx - 1) else set @slice = @String if(len(@slice)>0) insert into @temptable(Items) values(@slice) set @String = right(@String,len(@String) - @idx) if len(@String) = 0 break end return end
FINAL RESULT

source share