CREATE FUNCTION [dbo].[ParmsToList] (@Parameters varchar(8000), @delimiter varchar(10) )
returns @result TABLE (Value varchar(100))
AS
begin
declare @dx varchar(9)
-- declare @loops int
--set @loops = 0
DECLARE @TempList table
(
Value varchar(100)
)
if @delimiter is null set @delimiter = ' '
if len(@delimiter) < 1 set @delimiter = ' '
set @dx = left(@delimiter, case when @delimiter = ' ' then 1 else len(@delimiter) end -1)
DECLARE @Value varchar(8000), @Pos int
SET @Parameters = LTRIM(RTRIM(@Parameters))+ @delimiter
SET @Pos = CHARINDEX(@delimiter, @Parameters, 1)
IF REPLACE(@Parameters, @delimiter, @dx) <> ''
BEGIN
WHILE @Pos > 0 -- AND @Loops < 100
BEGIN
--set @loops = @loops + 1
SET @Value = LTRIM(RTRIM(LEFT(@Parameters, @Pos - 1)))
IF @Value <> ''
BEGIN
INSERT INTO @TempList (Value) VALUES (CAST(@Value AS varchar)) --Use Appropriate conversion
END
SET @Parameters = SUBSTRING(@Parameters, @Pos+ case when @delimiter = ' ' then 1 else len(@delimiter) end, 8000)
SET @Pos = CHARINDEX(@delimiter, @Parameters, 1)
END
END
INSERT @result
SELECT value
FROM @TempList
RETURN
END
|