Friday, July 8, 2011

Spliting String in SQL Server Stored Procedure

declare @par varchar(255)
set@par = 'string1, string2,string3'

declare @val as varchar(255)

create table #temp (col1 varchar(255))

while (charindex(',',@par)>0)

begin

insert into #temp

select Cast(replace(substring(@par,1,charindex(',',@par)),',','') as varchar)

set @par = substring(@par,charindex(',',@par)+1,len(@par))

end

insert
into #temp Select @par


Select * from #temp

drop table #temp

No comments: