How to Find and Replace Text in MySQL Database using SQL

»»»How to Find and Replace Text in MySQL Database using SQL
MySQL database has a handy and simple string function REPLACE() that allows table data with the matching string (from_string) to be replaced by new string (to_string). This is useful if there is need to search and replace a text string which affects many records or rows, such as change of company name, postcode, URL or spelling mistake.

The syntax of REPLACE is:

REPLACE(text_string, from_string, to_string)

MySQL reference describes REPLACE as function that returns the string text_string with all occurrences of the string from_string replaced by the string to_string, where matching is case-sensitive when searching for from_string. text_string can be retrieved from the a field in the database table too.

Most SQL command can use REPLACE() function, especially SELECT and UPDATE manipulation statement.

For example, the syntax for UPDATE SQL command with using REPLACE function:

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, 'find this string', 'replace found string with this string');

As an example:

update client_table set company_name = replace(company_name, 'Old Company', 'New Company')

The above statement will replace all instances of ‘Old Company’ to ‘New Company’ in the field of company_name of client_table table.

Another example for SQLECT query:

SELECT REPLACE('', 'w', 'Ww');

Above statement will return ‘’ as result. The REPLACE function is multibyte safe.

By |2016-12-09T08:40:08+00:00December 9th, 2016|Categories: Databases|Tags: , , |77 Comments

About the Author:

LK is a technology writer for Tech Journey with background of system and network administrator. He has be documenting his experiences in digital and technology world for over 15 years.Connect with LK through Tech Journey on Facebook, Twitter or Google+.