Created
August 18, 2014 17:55
-
-
Save rnique/cb9fb4ef486efe0f66dc to your computer and use it in GitHub Desktop.
How to Split a string by delimited char in SQL Server
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].[fnSplitString] | |
( | |
@string NVARCHAR(MAX), | |
@delimiter CHAR(1) | |
) | |
RETURNS @output TABLE(splitdata NVARCHAR(MAX) | |
) | |
BEGIN | |
DECLARE @start INT, @end INT | |
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string) | |
WHILE @start < LEN(@string) + 1 BEGIN | |
IF @end = 0 | |
SET @end = LEN(@string) + 1 | |
INSERT INTO @output (splitdata) | |
VALUES(SUBSTRING(@string, @start, @end - @start)) | |
SET @start = @end + 1 | |
SET @end = CHARINDEX(@delimiter, @string, @start) | |
END | |
RETURN | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Taken from:
http://www.sqlservercentral.com/blogs/querying-microsoft-sql-server/2013/09/19/how-to-split-a-string-by-delimited-char-in-sql-server/