Created
September 14, 2022 14:46
-
-
Save fesenpav/3ce0e2e3bb7c4667500b75a0d1b655c4 to your computer and use it in GitHub Desktop.
STRING_SPLIT function represented by user-created function, for databases with lower compatibility level
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].[StringSplit] ( | |
@stringToSplit NVARCHAR(MAX), | |
@splitCharacter NVARCHAR(1) | |
) | |
RETURNS | |
@returnList TABLE ([Name] [nvarchar] (500)) | |
AS | |
BEGIN | |
DECLARE @name NVARCHAR(255) | |
DECLARE @pos INT | |
DECLARE @NewString nvarchar(max) | |
SELECT @NewString = (SELECT REPLACE(@stringToSplit,' ','')) | |
WHILE CHARINDEX(@splitCharacter, @NewString) > 0 | |
BEGIN | |
SELECT @pos = CHARINDEX(@splitCharacter, @NewString) | |
SELECT @name = SUBSTRING(@NewString, 1, @pos-1) | |
INSERT INTO @returnList | |
SELECT @name | |
SELECT @NewString = SUBSTRING(@NewString, @pos+1, LEN(@NewString)-@pos) | |
END | |
INSERT INTO @returnList | |
SELECT @NewString | |
RETURN | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment