Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Extract values from a string using a delimiter Expand / Collapse
Author
Message
Posted Sunday, September 23, 2007 2:26 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Tuesday, February 05, 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 01, 2007 6:07 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

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 INT
DECLARE @Num_Fields INT

SET @String = 'A,B,C,D,E'
SET @Delimiter = ','
SET @Occurrence = 2
SET @Num_Fields = 2

DECLARE @s VARCHAR(255)
SET @String = @String + @Delimiter

; WITH
a 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) b
WHERE i BETWEEN o AND o + @Num_Fields - 1

PRINT @s -->B,C



Ryan Randall

Solutions are easy. Understanding the problem, now, that's the hard part.
Post #417478
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse