SET ANSI_NULLS ON;
GO
SET QUOTED_IDENTIFIER ON;
GO
-- =============================================
-- Author: blowersd
-- Create date: 20120821
-- Description: creates a quoted and delimited list from a string for use in a sql IN statement
-- delimeter default is comma
-- USAGE: SELECT DBO.FN_STRINGTOLIST('Ford,Lincoln,Mercury',',')
-- EXAMPLE: Select * from Atable A where A.columnx in dbo.fn_stringtolist(@ReportDropDownParameter)
-- =============================================
ALTER FUNCTION DBO.FN_STRINGTOLIST (
@PARMVARIABLE VARCHAR(4000)
,@DELIMITER VARCHAR(1) = ','
)
RETURNS VARCHAR(4050)
AS
BEGIN
DECLARE @OUTLIST VARCHAR(4050);
SET @DELIMITER = ISNULL(@DELIMITER, ',');
SET @OUTLIST = '''' + REPLACE(@PARMVARIABLE, ',', ''',''') + '''';
RETURN @OUTLIST;
END;