Skip to content

Instantly share code, notes, and snippets.

@kwestground
Last active October 13, 2023 13:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kwestground/080c354ad88cc6828ce1f4779b9f161c to your computer and use it in GitHub Desktop.
Save kwestground/080c354ad88cc6828ce1f4779b9f161c to your computer and use it in GitHub Desktop.
STRING_SPLIT for SQL 2012, 2014
CREATE FUNCTION [dbo].[STRING_SPLIT](
@string nvarchar(max),
@separator varchar(10))
RETURNS @returnList TABLE
(
[value] [nvarchar](500)
)
AS
BEGIN
DECLARE @name nvarchar(max);
DECLARE @pos int;
WHILE CHARINDEX(@separator, @string) > 0
BEGIN
SELECT @pos = CHARINDEX(@separator, @string);
SELECT @name = SUBSTRING(@string, 1, @pos - 1);
INSERT INTO @returnList
SELECT @name;
SELECT @string = SUBSTRING(@string, @pos + 1, LEN(@string) - @pos);
END;
INSERT INTO @returnList
SELECT @string;
RETURN;
END;
@semihozkul
Copy link

Thank you

@kwestground
Copy link
Author

Thank you

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment