I have adapted a function created by dinakar:CREATE FUNCTION dbo.fnParseArray (@array VARCHAR(1000),@separator CHAR(1))RETURNS @T Table (occ int,col1 varchar(50))AS BEGIN--DECLARE @T Table (col1 varchar(50))-- @Array is the array we wish to parse-- @Separator is the separator charactor such as a commaDECLARE @separator_position INT -- This is used to locate each separator characterDECLARE @array_value VARCHAR(1000) -- this holds each array value as it is returned-- For my loop to work I need an extra separator at the end. I always look to the-- left of the separator character for each array valuedeclare @occurence int =1 -- to get the Nth occurenceif (left(@array,1)=@separator)begin set @array=stuff(@array,1,1,'')endif (right(@array,1)<>@separator)begin SET @array = @array + @separatorend-- Loop through the string searching for separtor charactersWHILE PATINDEX('%' + @separator + '%', @array) <> 0 BEGIN-- patindex matches the a pattern against a stringSELECT @separator_position = PATINDEX('%' + @separator + '%',@array)SELECT @array_value = LEFT(@array, @separator_position - 1)-- This is where you process the values passed.INSERT into @T VALUES (@occurence,@array_value) set @occurence = @occurence + 1-- Replace this select statement with your processing-- @array_value holds the value of this element of the array-- This replaces what we just processed with and empty stringSELECT @array = STUFF(@array, 1, @separator_position, '')ENDRETURN ENDselect * from dbo.fnParseArray ('/23/34/45/67/89/101/','/')where occ=1
No, you're never too old to Yak'n'Roll if you're too young to die.