Skip to content

Instantly share code, notes, and snippets.

@LinZap
Created March 20, 2018 09:01
Show Gist options
  • Save LinZap/a25c904e008f8e421e1c092b34d42062 to your computer and use it in GitHub Desktop.
Save LinZap/a25c904e008f8e421e1c092b34d42062 to your computer and use it in GitHub Desktop.
create function fn_split(@str nvarchar(max),@separator as nvarchar(max))
returns @splits table(segment nvarchar(max))
as
begin
declare @anchor int=CHARINDEX(@separator,@str),@gram int=len(@separator)
while @anchor>0 and @gram<=len(@str)
begin
insert into @splits values(SUBSTRING(@str, 1, @anchor-1))
set @str=SUBSTRING(@str,@anchor+@gram,len(@str)-@anchor+@gram+1)
set @anchor=CHARINDEX(@separator,@str)
end;
insert into @splits values(@str)
return;
end
-- by Zap
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment