Sql Server function to convert comma separated value into tabular format
Create Function fn_GetSeprateValues(@Ids varchar(8000))
returns @Table Table(Id varchar(100))
As
Begin
Declare @ind int
Declare @Start int
Set @Ids=@Ids+','
Set @Start=1
Set @ind =charindex(',',@Ids)
While(@ind>0)
Begin
Declare @SubId varchar(100)
Set @SubId=Substring(@Ids,@Start,(@ind-1))
if(@SubId<>'' and @SubId Is Not Null)
Begin
insert into @Table(Id) values(@SubId)
End
Set @Ids =Substring(@Ids,@ind+1,Len(@Ids))
SET @ind = charindex(',',@Ids )
End
Return
End
I hope it make sense..