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.

39 Responses to “How to Find and Replace Text in MySQL Database using SQL”

Pages: 1 2

  1. ASP.NET SQL help
    February 8th, 2010 21:16
    39

    Great! Problem solved.

  2. Solution To WordPress getimagesize() Error « kyleabaker.com
    January 15th, 2010 06:37
    38

    [...] If there are matches found then you need to click the SQL tab at the top (preferably in a new tab) and paste the following (making sure to edit it to match the table and fields you need) and run it to find and replace that changed part of the path. update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘find this string’, ‘replace found string with this string’); For more information on SQL find and replace, either search google or read this. [...]

  3. Kim Woodbridge
    January 1st, 2010 23:39
    37

    Thanks! Just what I was looking for.

  4. Josh
    December 30th, 2009 05:12
    36

    this helped me a lot, thanks! :]

  5. Robert
    December 23rd, 2009 23:22
    35

    I need to replace some old links that are in the database records that used some javascript and contain % characters as part of the string. I can’t seem to get replace to recognize these and I am assuming it treats them like a wildcard char in mysql. I’ve tried escaping \% etc but not getting results.
    any suggestions

    example (these are not intended to be wildcard characters):
    update tablename set content = replace(content,’%200,%210,%200′,”);

  6. fisherw
    December 22nd, 2009 13:09
    34

    Remember that when using replace() to update a field (like the old company / new company example), the length of the field does not necessarily expand to fit the new content.

  7. Julio
    December 20th, 2009 02:57
    33

    This worked great for removing all the  characters that showed up on my wordpress blog after migrating my database into a mysql 5.0 db. Thank you!

  8. Stuart
    December 18th, 2009 02:52
    32

    Hi Thanks, just the ticket :)

  9. DjZoNe
    November 27th, 2009 21:49
    31

    This was pretty useful when I migrated standalone blogs to WPMU, and I needed to replace bunch of media file paths.

    Thanks.

  10. Vil Gays
    November 24th, 2009 00:00
    30

    @Richard:
    Just a guess: are you using regular single quotes (i.e. apostrophe) as you should, or maybe typographic ones, which you shouldn’t?

  11. Richard
    November 20th, 2009 14:16
    29

    I’m moving my blog to a new hosting company. Since I’m moving the blog to the root from a subfolder /blog/, I need to change the wp_posts guid field on all the posts.
    Example: http://mymarketingvp.net/blog/18-revision-5/ [This is from the current database.]

    Following the steps above, I entered this SQL statement:
    update wp_posts set guid = replace(guid, ‘net/blog/’, ‘net/’)
    ["net" is the end of the domain name *.net]

    I got the following error message:
    Error
    SQL query:
    UPDATE wp_posts SET guid = replace( guid, ‘net / blog / ’, ‘net / ’ )
    MySQL said:
    #1054 – Unknown column ‘‘net’ in ‘field list’

    Notice how it added some spaces in the command. Same error when I remove the spaces.

    Any suggestions??? Thanks!

  12. timtak
    October 28th, 2009 09:00
    28

    I am looking for a way to do this globally accross a whole database.

  13. kevin
    October 20th, 2009 20:28
    27

    Doesn’t work at all for replacing web addresses within a database.

  14. vishwas
    September 30th, 2009 15:26
    26

    Wanted to use REPLACE command to use for a column of a table…and this helped me…thanks

Pages: 1 2

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe to My Digital Life Comments RSS feed to receive notification of latest comments posted.

New Articles

Incoming Search Terms for the Article

mysql replace - mysql replace string - mysql replace text - sql find and replace - replace mysql - mysql find and replace - mysql string replace - mysql replace example - sql replace text - sql find replace - replace in mysql - mysql search and replace - sql search and replace - mysql update replace - mysql REPLACE syntax - find and replace sql - mysql search and replace text - replace mysql example - mysql find replace - mysql replace function - find and replace mysql - mysql find and replace text - mysql text replace - mysql replace character - find and replace sql query - Find and replace in SQL - find replace mysql - sql replace string - sql replace mysql - replace text mysql - sql replace command - sql search replace - search and replace mysql - find replace sql - mysql replace text in field - replace command in sql - replace string mysql - mysql find - sql replace - mysql sql replace - mysql find replace text - mysql search replace - mysql replace - update replace mysql - find and replace in mysql - mysql replace command - mysql replace string in field - REPLACE character mysql - find and replace in sql query - search and replace sql - sql query replace string - replace sql - sql query replace - mysql replace sql - mysql find and replace string - sql search and replace query - sql text replace - string replace mysql - sql replace syntax - mysql update string replace - mysql character replace - replace string in mysql - mysql remove character from string - replace text in mysql table - sql replace example - replace sql syntax - sql search replace string - find and replace mysql query - mysql sql replace function - mysql replace char - mysql search replace text - replace mysql syntax - FIND REPLACE IN mysql - mysql update replace character - replace text in mysql -