SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Extract values from a string using a delimiter


Extract values from a string using a delimiter

Author
Message
Tim Sneller
Tim Sneller
Forum Newbie
Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)Forum Newbie (3 reputation)

Group: General Forum Members
Points: 3 Visits: 3
Comments posted to this topic are about the item Extract values from a string using a delimiter
RyanRandall
RyanRandall
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4053 Visits: 4652
Just for fun BigGrin, 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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search