Find and replace string in database

Discussion in 'MySQL' started by Matt18, Apr 16, 2012.

  1. #1
    Hi

    I am trying to find and replace a string inside one column inside one table. Currently I am using:

    $sqlX = "UPDATE table SET column = replace(column,  'find_this_string', 'replace_with_this_string')";
    Code (markup):
    The problem is, I want to find this string anywhere in the content of the field. So for example:

    Replace here:
    Any content here find_this_string and any cotnent here.
    Code (markup):
    To make it look like here:
    Any content here replace_with_this_string and any cotnent here.
    Code (markup):
    My first guess would be something like:
    $sqlX = "UPDATE table SET column = replace(column,  '*find_this_string*', 'replace_with_this_string')";
    Code (markup):
    But this doesn’t work. Can someone please help me out?

    Thank you very much in advance!
     
    Matt18, Apr 16, 2012 IP
  2. Basti

    Basti Active Member

    Messages:
    625
    Likes Received:
    6
    Best Answers:
    3
    Trophy Points:
    90
    #2
    You could attach a WHERE clause where text LIKE

    
    $sqlX = "UPDATE table SET column = replace(column,  'find_this_string', 'replace_with_this_string') WHERE column LIKE '%find_this_string%'";
    
    Code (markup):
     
    Basti, Apr 16, 2012 IP
  3. hoangvu

    hoangvu Greenhorn

    Messages:
    75
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    Using php code str_replace() before add it to mysql statement
     
    hoangvu, Apr 16, 2012 IP
  4. Matt18

    Matt18 Guest

    Messages:
    591
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks! :)
     
    Matt18, Apr 18, 2012 IP