mySQL query to mass change all instances of a particular word.

Discussion in 'MySQL' started by tonyrocks, May 20, 2008.

  1. #1
    I was wondering if anybody could show me a quick query to change all instances in the database from tony to TONY (all caps)
     
    tonyrocks, May 20, 2008 IP
  2. apmsolutions

    apmsolutions Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Not sure of the best way, but you could generate a data script using mySQL administrator. Once you have that script, open it in your mySQL Query Browser, then to a search and replace for tony, checking the case sensitive option.

    Then run the script.
     
    apmsolutions, May 21, 2008 IP
  3. bluecape

    bluecape Peon

    Messages:
    89
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    maybe this php script can give you an idea.

    
    <?php
    
    $query = mysql_query("SELECT * FROM `db_table`");
    while($row = mysql_fetch_array($query))
    {
    	mysql_query("UPDATE `db_table` SET `name` = 'TONY' WHERE `name` = 'tony'");
    }
    
    ?>
    
    Code (markup):
     
    bluecape, May 22, 2008 IP
  4. Petey

    Petey Peon

    Messages:
    68
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    If you can run SQL directly against the database then you could use something like:

    update tablename set fieldname = case fieldname when 'tony' then 'TONY' else fieldname end;

    Backup/export the table first - this is very unforgiving of finger trouble!
     
    Petey, May 22, 2008 IP
  5. mwasif

    mwasif Active Member

    Messages:
    816
    Likes Received:
    23
    Best Answers:
    1
    Trophy Points:
    70
    #5
    The following query will replace tony with TONY in the specified field.
    UPDATE table SET 
    field_name = REPLACE(field_name, 'tony', 'TONY')
    Code (markup):
    Before running this query, backup your table.
     
    mwasif, May 22, 2008 IP
  6. apmsolutions

    apmsolutions Peon

    Messages:
    66
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I think he wants to replace all occurrences in all fields of all tables though.
     
    apmsolutions, May 26, 2008 IP
  7. Kuldeep1952

    Kuldeep1952 Active Member

    Messages:
    290
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    60
    #7
    You can export the data. Edit the data in a text Editor to carry out search and replace,
    and then re-import the data.
     
    Kuldeep1952, May 27, 2008 IP
  8. tonyrocks

    tonyrocks Active Member

    Messages:
    1,574
    Likes Received:
    50
    Best Answers:
    0
    Trophy Points:
    88
    #8
    You know, I actually found some indexing tricks that worked. Woowhoo.. thanks for all the help!
     
    tonyrocks, May 27, 2008 IP
  9. karlbryan

    karlbryan Peon

    Messages:
    92
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Can you share that to us? I'm also looking for a query like that.
     
    karlbryan, Mar 13, 2010 IP