Search

Friday, November 5, 2010

Comma delimited string to table

CREATE FUNCTION [dbo].[ufn_ConvCSVToTable] ( @strInput VARCHAR(8000) )
RETURNS @tblTable TABLE ( [String] VARCHAR(10) )
AS
BEGIN

DECLARE @strTemp VARCHAR(10)

WHILE LEN(@strInput) > 0
BEGIN
SET @ strTemp = LEFT(@strInput,
ISNULL(NULLIF(CHARINDEX(',', @ strInput) - 1, -1),
LEN(@strInput)))
SET @ strInput = SUBSTRING(@strInput,
ISNULL(NULLIF(CHARINDEX(',', @ strInput), 0),
LEN(@strInput)) + 1, LEN(@strInput))

INSERT INTO @ tblTable ( [String] )
VALUES ( @ strTemp)
END

RETURN
END
GO

No comments:

Post a Comment