Created
June 2, 2015 00:48
-
-
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.
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].[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