Created
January 29, 2014 21:45
-
-
Save mxalix258/8697856 to your computer and use it in GitHub Desktop.
Function to parse a delimited string and return a table
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
--declare a table where the parsed values can be stored | |
DECLARE @ParsedStringTable TABLE | |
( | |
--set a column named "Source" | |
Source varchar(20) | |
) | |
INSERT INTO @ParsedStringTable | |
-- pass delimited string and delimter into function (CKI+FEE+IHS, '+') | |
-- Select the string table returned from the function (line 2) | |
SELECT string FROM dbo.fnParseString (@DelimitedStringParameter , ''+'')' |
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 fnParseString (@string NVARCHAR(MAX),@separator NCHAR(1)) | |
RETURNS @parsedString TABLE (string NVARCHAR(MAX)) | |
AS | |
BEGIN | |
DECLARE @position int | |
SET @position = 1 | |
SET @string = @string + @separator | |
WHILE charindex(@separator,@string,@position) <> 0 | |
BEGIN | |
INSERT into @parsedString | |
SELECT substring(@string, @position, charindex(@separator,@string,@position) - @position) | |
SET @position = charindex(@separator,@string,@position) + 1 | |
END | |
RETURN | |
END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment