MySQL String Replace

While messing around with a PHP script for string replacement (using the str_replace function) in a MySQL database field, I came across a way to find and replace the string by using only MySQL. Maybe it’s just my ignorance of MySQL’s capabilities, but I didn’t think there would be anyway to do this without the use of PHP or any other scripting language. In this case I was trying to remove a domain name from an entire column for the purpose of converting absolute URLs to relative URLs. Anyway, I thought I would post it here for future reference.

If you run the following query:

UPDATE table_name SET column_name = replace(column_name,’’, ”)WHERE column_name LIKE ‘%’;

It will take URLs like this:

And change them to:


Of course it can be done with any kind of string that appears in many fields, not just URLs.

Comments are closed.

Recent Cycling

  • 7.82 miles on 03/11/18
  • 6.79 miles on 03/10/18
  • 4.80 miles on 02/04/18
  • 31.90 miles on 09/15/17
This Month:

14.61 miles

Last Month:

4.80 miles

This Year:

19.41 miles

Last Year:

354.90 miles