Results 1 to 2 of 2
  1. #1
    Join Date
    Oct 2008

    Unanswered: Replace first part of string


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




    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!


  2. #2
    Join Date
    Apr 2002
    Toronto, Canada
    that guelphdad, he's such a kidder, eh?

    actually, SUBSTRING_INDEX might work here

    do you know how it works? | @rudydotca
    Buy my SitePoint book: Simply SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts