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


Replacing strings


Replacing strings

Author
Message
ramadesai108
ramadesai108
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2399 Visits: 690
I have the following string:

James Murphy;#278;#Leslie Owens;#275;#Rita Sallam;#279;#Stephen Powers;#277;#Susan Feldman;#68

I need to replace ";#278;#" with ","
replace ";#275;#" with ","
replace ";#279;#" with ","
replace ";#277;#" with ","
replace ";#68" with ","

In other words, I need to replace a pattern with ";#" + number + ";#"
but the last pattern does not have ";#"

How do I do this?
bitbucket-25253
bitbucket-25253
SSCoach
SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)SSCoach (15K reputation)

Group: General Forum Members
Points: 15559 Visits: 25280
This might help you:
SET @Str = 'James Murphy;#278;#Leslie Owens;#275;#Rita Sallam;#279;#Stephen Powers;#277;#Susan Feldman;#68'

--CHAR(59) is a ; CHAR(35) is #

SET @Str = REPLACE(@Str,CHAR(59) + CHAR(35),',')
WHILE PATINDEX('%[0-9]%',@Str) > 0
BEGIN
SET @Len = LEN(@Str)
SET @T = SUBSTRING(@Str,PATINDEX ( '%[0-9]%' , @Str),PATINDEX ( '%[0-9]%' , @Str)+1)
IF @T = 0
BREAK
ELSE
SET @Str = REPLACE(@Str,@T,'')
CONTINUE
END
SET @Str = REPLACE(@Str,',,',',')
SELECT @Str AS 'Final'



Giving you:
James Murphy,Leslie Owens,Rita Sallam,Stephen Powers,Susan Feldman,

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
ramadesai108
ramadesai108
SSCrazy
SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)SSCrazy (2.4K reputation)

Group: General Forum Members
Points: 2399 Visits: 690
Hi BitBucket,
That did help. I would have to change it a little, but thank you much for your time. I appreciate it.
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