Mike Rogers on a Snowmobile

Mike Rogers

Web Developer

MySQL REPLACE function

Posted on

MySQL always surprises me in the sheer amount of stuff you can do in it. For example I recently found it has a bunch of String Functions. The main one I found to be useful was the REPLACE function, which works like this:

REPLACE('Original String', 'Find This', 'Replace with this')

# So it can be used like:

SELECT REPLACE('My Original String', 'Original', 'Modified');
# This will output: My Modified String

# So if you wanted to go through a full  table and replace a bunch of strings, you could use it like this:

UPDATE `table_name` SET `field_name` = REPLACE(`field_name`, 'Find Me', 'Replace with Me');
View on Gist Github

This was written by Mike Rogers, a freelance Ruby on Rails developer based in London.

Share the ♥ by sharing this!

If you want to discuss this post, feel free to tweet me (@MikeRogers0) or drop me an email. Any code samples unless stated otherwise are licensed under the The MIT License (MIT). Spotted a mistake? Send me a pull request :)