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.


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

Pages: [2] 1 » Show All

  1. 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?

  2. 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!

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

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

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

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

  5. 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: [2] 1 » Show All

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 comments feature has been disabled. To receive notification of latest comments posted, subscribe to My Digital Life Comments RSS feed or register to receive new comments in daily email digest.
Custom Search

New Articles

Incoming Search Terms for the Article

mysql replace - MySQL replace text - sql find and replace - mysql replace string - replace mysql - mysql find and replace - mysql replace example - sql find replace - sql search and replace - sql replace text - mysql string REPLACE - mysql update replace - mysql find replace - replace in mysql - find and replace sql - sql replace command - MySQL search and replace - find and replace in sql - find and replace mysql - sql replace - MYSQL REPLACE SYNTAX - sql search replace - mysql replace character - find replace sql - mysql text replace - mysql replace function - mysql find - replace mysql example - mysql find and replace text - find replace mysql - mysql replace command - mysql REPLACE - sql find and replace query - replace sql - search and replace sql - sql replace string - mysql search and replace string - replace mysql syntax - replace text mysql - mysql sql replace - update replace mysql - replace command in sql - mysql search replace - sql string replace - FIND AND REPLACE IN MYSQL - mysql update replace string - mysql find replace text - replace sql mysql - search and replace mysql - search replace sql -