Skip to content

Instantly share code, notes, and snippets.

@mxalix258
Created January 29, 2014 21:45
Show Gist options
  • Save mxalix258/8697856 to your computer and use it in GitHub Desktop.
Save mxalix258/8697856 to your computer and use it in GitHub Desktop.
Function to parse a delimited string and return a table
--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 , ''+'')'
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