Skip to content

Instantly share code, notes, and snippets.

@fesenpav
Created September 14, 2022 14:46
Show Gist options
  • Save fesenpav/3ce0e2e3bb7c4667500b75a0d1b655c4 to your computer and use it in GitHub Desktop.
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
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