Tuesday, March 1, 2011

MySql Question - way to update portion of a string?

I'm looking for a way to update just a portion of a string via mysql query.

For example, if I have 10 records all containing 'string' as part of the field value (i.e., 'something/string', 'something/stringlookhere', 'something/string/etcetera', is there a way to change 'string' to 'anothervalue' for each row via one query, so that the result is 'something/anothervalue', 'something/anothervaluelookhere', 'something/string/etcetera', is there a way to change 'anothervalue'

From stackoverflow
  • UPDATE `table` SET `field` = REPLACE(`field`, `string`, `anothervalue`)
    
  • use a LIKE operator to find the rows that you care about, and update them using the REPLACE function.

    OMG Ponies : FYI: code > explanation, but both is ideal.
  • I think this should work:

    update table set field = REPLACE(field, 'string', 'anothervalue') WHERE field LIKE '%string%';
    
    n00b0101 : Of course... thank you... Once upon a time, I knew about this function... stupid flu.

0 comments:

Post a Comment