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.
Related Articles
- Remove or Trim First or Last Few Characters in MySQL Database with SQL
- Check and Optimize MySQL Database Automatically with Crontab/Cron
- MySQL Error 1170 (42000): BLOB/TEXT Column Used in Key Specification Without a Key Length
- MySQL Database Performance Tuning Best Practices Video Tutorial
- Enable Logging of Slow Queries (Slow Query Log) in MySQL Database
- Change and Reset MySQL root Password
- Replace Notepad with Another Text Editor (eg. Notepad2 and Notepad++) in Vista
- Download File Content Replacer to Search and Replace Text in Multiple Files
- Using PHP-MySQL Persistent Connections to Run WordPress Blog
- How to Backup and Restore (Export and Import) MySQL Databases Tutorial










































September 15th, 2009 19:14
Thanks.
September 7th, 2009 20:44
thanks men for the help
September 1st, 2009 14:08
This is pretty good. I use replace function very often. somehow I wish it had the capability to replace multiple values (different values) at once without having to nest them.
July 14th, 2009 08:08
Thanks!
I used this
July 8th, 2009 22:11
Am very thankful, The code helped me. Google diverted me to this post. Thank you again.
June 30th, 2009 16:19
Thanks for sharing
unibet
June 27th, 2009 23:35
[...] The guide uses SQL statements based on MySQL replace() functionto modify the database. To run SQL queries, login to MySQL database that houses WordPress tables via [...]
May 13th, 2009 14:40
hey guys…
I want to update my joomla cotnent with another column data
eg.
mysql_query(”update jos_content set introtext = replace(introtext,’XXXXXXXX’,'DATA FROM ANOTHER COLUMN’) where SECID = ‘1′ or die(mysql_error())//;
DATA FROM ANOTHER COLUMN == SN column from same table…..
cAN YOU PLEASE HELP ME IN CORRECT SQL SYNTAX
April 22nd, 2009 04:59
[...] I got the info from http://www.mydigitallife.info/2007/04/23/how-to-find-and-replace-text-in-mysql-database-using-sql/ [...]
March 18th, 2009 17:47
Thanks 4 the help!
March 7th, 2009 04:22
Thank you very much. Exactly what I needed!
February 26th, 2009 17:13
[...] 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 [...]
February 20th, 2009 17:18
When I try this I get this message…
“Argument data type text is invalid for argument 1 of replace function”.
February 7th, 2009 06:52
[...] referred to that old domain. So I was delighted when I found a website that included the necessary SQL to find and replace. This has saved me a lot of [...]
November 29th, 2008 00:46
didn’t work for me…
October 21st, 2008 09:42
[...] quick Google search led me to this helpful page. In short, I had to run the following: UPDATE wp_posts SET post_content = [...]
October 16th, 2008 01:00
For me this is only working on exact replaces. I could replace all entries that are exact the searchterm but i could replace parts of an entry.
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
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).
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 [...]
June 4th, 2008 15:43
thanks, this is very helpful!!
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 [...]
May 16th, 2007 07:24
Excellent, just what I needed. Thanks!
May 11th, 2007 10:38
Thanks for the help!
May 4th, 2007 05:48
Thanks a lot! You’ve saved my life!