Skip to content

Instantly share code, notes, and snippets.

@hoangitk
Created September 9, 2014 02:17
Show Gist options
  • Save hoangitk/8c5642f59ca1d79e2068 to your computer and use it in GitHub Desktop.
Save hoangitk/8c5642f59ca1d79e2068 to your computer and use it in GitHub Desktop.
CREATE FUNCTION SplitString
(
@Input NVARCHAR(MAX),
@Character CHAR(1)
)
RETURNS @Output TABLE (
Item NVARCHAR(1000)
)
AS
BEGIN
DECLARE @StartIndex INT, @EndIndex INT
SET @StartIndex = 1
IF SUBSTRING(@Input, LEN(@Input) - 1, LEN(@Input)) <> @Character
BEGIN
SET @Input = @Input + @Character
END
WHILE CHARINDEX(@Character, @Input) > 0
BEGIN
SET @EndIndex = CHARINDEX(@Character, @Input)
INSERT INTO @Output(Item)
SELECT SUBSTRING(@Input, @StartIndex, @EndIndex - 1)
SET @Input = SUBSTRING(@Input, @EndIndex + 1, LEN(@Input))
END
RETURN
END
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment