T-SQL function to Parse Delimited String : T-SQL Code

T-SQL function to Parse Delimited String : T-SQL Code

CREATE FUNCTION [dbo].[fn_Parse_DelimitedString]
(
@DelimattedString VARCHAR(8000),
@Delimiter CHAR(1)
)
RETURNS @OutputTable TABLE ( ITEMNAME VARCHAR(100) )
AS
BEGIN
DECLARE @String VARCHAR(100)
WHILE LEN(@DelimattedString) > 0

BEGIN

SET @String = LEFT(@DelimattedString,

ISNULL(NULLIF(CHARINDEX(@Delimiter, @DelimattedString) - 1, -1),

LEN(@DelimattedString)))

SET @DelimattedString = SUBSTRING(@DelimattedString,

ISNULL(NULLIF(CHARINDEX(@Delimiter, @DelimattedString), 0),

LEN(@DelimattedString)) + 1, LEN(@DelimattedString))

INSERT INTO

@OutputTable ( ITEMNAME )

VALUES

( @String )

END

RETURN

END;

No comments: