Category — How To:

How To: Replace a Partial String in MySQL

When developing web sites, sometimes things change from how they were in development to how they need to be in production, especially when it comes to machine names and IP addresses, etc.  If these values are stored in a database, chances are you’ll need some quick SQL magic to help finalize the migration.

For those of you that just want the info, here you go.  This is a quick and easy way to directly replace one string with another for all rows in a column/field.  DISCLAIMER: If you don’t know what you are doing, you can really screw up your database.  Please take care to do a backup and by all means, if you can’t fix what you break, don’t attempt this.

UPDATE MyDB.MyTable
SET
MyDB.MyTable
= REPLACE(MyDB.MyTable.MyField,'OldString','NewString')
WHERE
MyDB.MyTable.MyField like '%OldString%';

[Read more →]

January 1, 2009   13 Comments