Use Case
Split string like 'applicationId=1&objectId=2' to pairs applicationId = 1, objectId=2 in sql server stored proc
Solution
Following solution works fine for me:
CREATE FUNCTION [dbo].[fn_HashSplit]
(
@String varchar(max),
@Delimiter1 char(1),
@Delimiter2 char(1)
)
RETURNS
@temptable TABLE (
name varchar(8000) COLLATE Latin1_General_CI_AS_KS_WS,
value varchar(8000) COLLATE Latin1_General_CI_AS_KS_WS
)
AS
BEGIN
declare @idx int
declare @slice varchar(8000)
declare @idx2 int
declare @name varchar(8000)
declare @value varchar(8000)
select @idx = 1
if len(@String)<1 or @String is null return;
while @idx!= 0
begin
set @idx = charindex(@Delimiter1,@String)
if @idx!=0
set @slice = left(@String,@idx - 1)
else
set @slice = @String
if(len(@slice)>0)
begin
select @idx2 = charindex(@Delimiter2, @slice)
if @idx2 != 0
begin
set @name = left(@slice, @idx2-1)
set @value = right(@slice, len(@slice) - @idx2)
insert into @temptable(name, value) values(@name, @value)
end
end
set @String = right(@String,len(@String) - @idx)
if len(@String) = 0 break
end
return
END