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 be REPLACE() function, especially SELECT and UPDATE manipulation statement.
For example:
update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’);
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:
SELECT REPLACE(’www.mysql.com’, ‘w’, ‘Ww’);
Above statement will return ‘WwWwWw.mysql.com’ as result.
Share and contribute or get technical support and help at My Digital Life Forums.
Related Articles
- Change and Reset MySQL root Password
- How to Move WordPress Blog to New Domain or Location
- MySQL Database Performance Tuning Best Practices Video Tutorial
- Check and Optimize MySQL Database Automatically with Crontab/Cron
- Install Web Server in Windows XP with Apache2, PHP5 and MySQL4 - Part 4
- How to Backup and Restore (Export and Import) MySQL Databases Tutorial
- MySQL Error 1170 (42000): BLOB/TEXT Column Used in Key Specification Without a Key Length
- Change or Set MySQL Long Query Time Value for log-slow-queries
- Remove or Trim First or Last Few Characters in MySQL Database with SQL
- Installing Web Server in FreeBSD 6.0 with Apache 2.2, MySQL 5.0 and PHP 5 - Part 3

































May 4th, 2007 05:48
Thanks a lot! You’ve saved my life!
May 11th, 2007 10:38
Thanks for the help!
May 16th, 2007 07:24
Excellent, just what I needed. Thanks!
October 1st, 2007 18:23
[...] The guide uses SQL statements based on MySQL replace() function to modify the database. To run SQL queries, login to MySQL database that houses WordPress tables [...]
June 4th, 2008 15:43
thanks, this is very helpful!!
September 1st, 2008 01:48
[...] The guide uses SQL statements based on MySQL replace() function to modify the database. To run SQL queries, login to MySQL database that houses WordPress tables [...]
September 10th, 2008 21:52
Algo k es puede resultar practico es cuando se necesita eliminar un caracter de una sere de registros es:
update tabla1 set campo1=replace(campo1,’8′,”);
esta sentencia eliminaria todos los 8 de la tabla: tabla1 del registro: campo1
(NOTA: las comillas son todas simples).
October 9th, 2008 21:16
nececito reemplazar un tag con todo su relleno, por ejemplo
update pagina_web.noticias set texto = replace(texto, “”>”,” ” )
por vacio!!! como hago para estandalizarla y que encuetre todo lo que esta dentro de