Remove instances of a string in a MySQL table

Discussion in 'Programming' started by archard, Jan 29, 2008.

  1. #1
    I have a table called "drupal_term_data" and a field in that table called "name". What I'd like is a query I can run that would remove the word "Tabs" from every record in that field. Can this be done?
     
    archard, Jan 29, 2008 IP
  2. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Are you wanting to delete the whole row, delete that field only or simply remove the word tabs?
     
    AstarothSolutions, Jan 29, 2008 IP
  3. drcode

    drcode Peon

    Messages:
    24
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Run your mySQL query to select the name field, and index field, of all the records.


    looping through all the records:
    Use preg_replace() www.php.net/preg-replace to remove "Tabs"
    UPDATE the field

    Caveats:

    How many records?
    Do you want to remove a space with the word?
    Does the table have an index or unique field?

    Michael
     
    drcode, Jan 29, 2008 IP
  4. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can do it with pure SQL very easily, just not sure which of the 3 they want to do
     
    AstarothSolutions, Jan 29, 2008 IP
  5. archard

    archard Peon

    Messages:
    221
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Sorry if I wasn't clear, I'm not totally sure of the terminology... I think I got it right now though. Inside the drupal_term_data table there is a column called "name", with 529 rows. Each row contains a string of text with the word "Tabs" on the end. I'd like to run an SQL query that can remove "Tabs" from each row (along with the space before it). The field "tid" is the primary key, "vid" is the index key.

    That should answer your questions too, drcode.
     
    archard, Jan 30, 2008 IP
  6. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #6
    UPDATE drupal_term_data SET name = TRIM(TRAILING ' Tabs' FROM name);
    Code (SQL):
    It has been a long time since I did MySQL so cannot remember if you need to put markers around column names etc but I am sure you get the idea
     
    AstarothSolutions, Jan 30, 2008 IP
    archard likes this.
  7. archard

    archard Peon

    Messages:
    221
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Perfect! Thank you.
     
    archard, Jan 30, 2008 IP
  8. AstarothSolutions

    AstarothSolutions Peon

    Messages:
    2,680
    Likes Received:
    77
    Best Answers:
    0
    Trophy Points:
    0
    #8
    No problem - have to say that far too many people argue how great MySQL is (and it is actually very good even if we use MS SQL or Oracle) but then dont know how to use 10% of its capabilities.
     
    AstarothSolutions, Jan 30, 2008 IP