Recent PostsRecent Posts Popular TopicsPopular Topics
 Home Search Members Calendar Who's On

 Extract values from a string using a delimiter Rate Topic Display Mode Topic Options
Author
 Message
 Posted Sunday, September 23, 2007 2:26 AM
 Forum Newbie Group: General Forum Members Last Login: Tuesday, February 5, 2008 2:57 AM Points: 1, Visits: 3
 Comments posted to this topic are about the item Extract values from a string using a delimiter
Post #401518
 Posted Thursday, November 1, 2007 6:07 AM
 SSCommitted Group: General Forum Members Last Login: Tuesday, May 29, 2012 11:22 AM Points: 1,755, Visits: 4,652
 Just for fun :D, here's the same sort of thing using the recursive CTE technique...DECLARE @String VARCHAR(255)DECLARE @Delimiter CHAR(1)DECLARE @Occurrence INTDECLARE @Num_Fields INTSET @String = 'A,B,C,D,E'SET @Delimiter = ','SET @Occurrence = 2SET @Num_Fields = 2DECLARE @s VARCHAR(255)SET @String = @String + @Delimiter; WITHa AS ( SELECT CAST(NULL AS VARCHAR(255)) AS x, @String y, 0 AS i UNION ALL SELECT LEFT(y, CHARINDEX(',', y)-1), SUBSTRING(y, CHARINDEX(',', y)+1, 255), i + 1 FROM a WHERE y <> '')SELECT @s = ISNULL(@s + ',', '') + x FROM a, (SELECT CASE WHEN @Occurrence < 0 THEN COUNT(*) + @Occurrence ELSE @Occurrence END AS o FROM a) bWHERE i BETWEEN o AND o + @Num_Fields - 1PRINT @s -->B,C Ryan RandallSolutions are easy. Understanding the problem, now, that's the hard part.
Post #417478

 Permissions