in this article, i will show you, split string delimiter function in sql server
create function splitstring(@string varchar(8000), @delimiter varchar(50))
returns @results table (items varchar(8000))
as
begin
declare @index int
declare @slice varchar(8000)
-- have to set to 1 so it doesnt equal z
-- first time in loop
select @index = 1
if @string is null return
while @index !=0
begin
-- get the index of the first occurence of the split character
select @index = charindex(@delimiter,@string)
-- now push everything to the left of it into the slice variable
if @index !=0
select @slice = left(@string,@index - 1)
else
select @slice = @string
-- put the item into the results set
insert into @results(items) values(@slice)
-- chop the item removed off the main string
select @string = right(@string,len(@string) - @index)
-- break out if we are done
if len(@string) = 0 break
end
return
end
example:
select * from splitstring(@param,',')
create function splitstring(@string varchar(8000), @delimiter varchar(50))
returns @results table (items varchar(8000))
as
begin
declare @index int
declare @slice varchar(8000)
-- have to set to 1 so it doesnt equal z
-- first time in loop
select @index = 1
if @string is null return
while @index !=0
begin
-- get the index of the first occurence of the split character
select @index = charindex(@delimiter,@string)
-- now push everything to the left of it into the slice variable
if @index !=0
select @slice = left(@string,@index - 1)
else
select @slice = @string
-- put the item into the results set
insert into @results(items) values(@slice)
-- chop the item removed off the main string
select @string = right(@string,len(@string) - @index)
-- break out if we are done
if len(@string) = 0 break
end
return
end
example:
select * from splitstring(@param,',')
0 Comments
if you have any doubts , please let me know