Last active
October 13, 2023 13:43
-
-
Save kwestground/080c354ad88cc6828ce1f4779b9f161c to your computer and use it in GitHub Desktop.
STRING_SPLIT for SQL 2012, 2014
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you