Skip to content

Instantly share code, notes, and snippets.

@jbnv
Created June 2, 2015 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbnv/7c6cf0ce7d2844dead7b to your computer and use it in GitHub Desktop.
Save jbnv/7c6cf0ce7d2844dead7b to your computer and use it in GitHub Desktop.
Function for SQL Server that splits a delimited string into a table of items.
CREATE FUNCTION [dbo].[Split] (
@List nvarchar(MAX),
@Delimiter nvarchar(3)
)
RETURNS TABLE
WITH SCHEMABINDING
AS
RETURN (
SELECT ROW_NUMBER() OVER(ORDER BY N) AS [Ordinal],
SUBSTRING(@List, N, CHARINDEX(@Delimiter, @List + @Delimiter, N) - N) AS [Item]
FROM dbo.Numbers
WHERE N <= CONVERT(INT, LEN(@List))
AND SUBSTRING(@Delimiter + @List, N, LEN(@Delimiter)) = @Delimiter
);
GO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment