GO
IF OBJECT_ID(N'udf__ListToSingleColumn') IS NOT NULL
DROP FUNCTION udf__ListToSingleColumn
GO
SET ANSI_NULLS OFF
GO
SET QUOTED_IDENTIFIER OFF
GO
-- *****************************************************************************
-- Name : udf__ListToSingleColumn
-- Author : Chandra Gunawan
-- Date : 02-Jun-2009
-- Description : Convert comma seperated list of item to table
-- Parameters :
-- @pList vchr(8000) Delimited list of item to split
-- format: []
-- example: a,b,c
-- @pDelimiter vchr(1) Delimiter (dlm)
--
-- Usage Sample :
-- -------------------------------------------------
-- SELECT * FROM dbo.udf__ListToSingleColumn('a,b,c', ',')
-- -------------------------------------------------
--
-- Maintenance Log
-- =============================================================================
-- Date ID Description
-- -----------------------------------------------------------------------------
-- 02-Jun-09 chg Initial Version
-- *****************************************************************************
CREATE FUNCTION dbo.udf__ListToSingleColumn
(
@pList varchar(8000),
@pDelimiter varchar(1) = ','
)
RETURNS @pTable TABLE
(
item varchar(1000)
)
BEGIN
DECLARE @iSpot smallint,
@cStr varchar(8000),
@cSql varchar(8000)
IF ISNULL(@pDelimiter, '') = '' SET @pDelimiter = ','
WHILE @pList <> ''
BEGIN
SET @iSpot = CHARINDEX(@pDelimiter, @pList)
IF @iSpot > 0
BEGIN
SET @cStr = LEFT(@pList, @iSpot - 1)
SET @pList = RIGHT(@pList, len(@pList) - @iSpot)
END
ELSE BEGIN
SET @cStr = @pList
SET @pList = ''
END
INSERT @pTable SELECT @cStr
END
RETURN
END
GO
GRANT SELECT ON udf__ListToSingleColumn TO public
GO
No comments:
Post a Comment