Matt Stenson

This is the personal site of Matt Stenson. I'm a programmer, nerd, mountain biker, skier and outdoors enthusiest. I'll blog about different stuff here. I like open software and privacy focused tools.

Replace a string inside of a MYSQL field

Reading Time: 1 min

I recently needed to replace part of a URL found inside the body HTML of a page that was stored in a MySQL column.  In order to do this I was able to use the handy Replace() MySQL function.

SET pageBody = REPLACE(pageBody, '/oldirectory', '/newdirectory') 
WHERE INSTR(pageBody, '/olddirectory') > 0;

The INSTR() function is not technically needed, but it prevents MySQL from doing an update on every row regardless of if the string is found.

See Also