If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Replace first part of string

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-15-08, 08:56
mwouters mwouters is offline
Registered User
 
Join Date: Oct 2008
Posts: 1
Replace first part of string

Hello,

I want to replace all /thiswebsite/ to /otherwebsite/ in a field called "url" of a mysql databasetable that for example contain:

/thiswebsite/link_to_page1/subpage1

/thiswebsite/link_to_page2/subpage1

/thiswebsite/link_to_page1/subpage2

So with a query i need to replace /thiswebsite/ in all records where the "url" field STARTS with /thiswebsite/ to /otherwebsite/...
It is imortant to replace it ONLY where it occurs at the start of the url.

The REPLACE function in MySQL does not seem to be suitable since it will replace the value everywhere?

Someone told me to look at substring_index.
I'm not an expert though, so I wonder if this would do the trick, and if not if someone can help me?

UPDATE dbtable SET url = REPLACE((SELECT SUBSTRING_INDEX(url, '/thiswebsite/', 1)), '/thiswebsite/', '/otherwebsite/');

Thanks a lot!

Mark.
Reply With Quote
  #2 (permalink)  
Old 10-15-08, 13:00
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
that guelphdad, he's such a kidder, eh?

actually, SUBSTRING_INDEX might work here

do you know how it works?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On