Friday, October 1, 2010

mysql string search and replace

UPDATE [tablename] SET [fieldname] = REPLACE([fieldname],"[oldstring]","[newstring]");

The code above would operate on every row in the table but you may want WHERE conditions. Example:
UPDATE profile SET homepage = REPLACE(homepage,"blargh.example.com","foo.example.com") WHERE first_name="Joe" AND last_name="Schmoe";

Will change Joe's homepage to http://foo.example.com/ instead of http://blargh.example.com/.

No comments:

Post a Comment