// 12.Jul.2008

MySQL Search and Replace

MySQL

Performing a case-sensitive search-and-replace through a table is easy with MySQL when you know how.
update table_name set table_field = replace(table_field,'replace_that','with_this');
So, for example, let’s say you have a table called “posts” with a data stored in a field called “con­tent” and you want to replace all instances of the word “dog” with “cat”, then your SQL would look like this:
update posts set content = replace(content,'dog','cat');
How cool is that?

Last Revision: October 9th, 2009 at 09:12
Short URL: http://wp.me/phEOu-1l (Tweet This!)


32 Comments for “MySQL Search and Replace”

  1. Hey, thanks for put­ting this back up. I use this tidbit of code every few months — infre­quently enough that I never remember the exact syntax when I need it, but often enough that living without it would be a hassle. I know I should just save the snippet on my drive some­where… but I’d never be able to find it again; it’s much easier to search my book­marks for ‘urb­an­main­frame’. :)

    (In the time after you remod­elled the site but before you readded this post, I actu­ally found it at archive.org, using the old link, simply because I had been too stupid to save a copy. One of these days, I’ll do that. In the mean­time… thanks. :) )

  2. No problem at all Claude. I’m glad the inform­a­tion is useful to you. I had to repost this page as it seems to have been the most requested page from the old site — and I got sick of seeing all those 404 errors in my logs. “MySQL search & replace” is also the most pop­ular search query that brings new readers to the site.

    In a way it’s kind of dis­heart­ening that, of all the posts I pre­vi­ously pub­lished, only one seems to have been appre­ci­ated! Oh well, per­haps I’ll have more luck this time round eh? :-)

  3. I’m so thankful that you did put the page back up! I have the same issue that Claude has, in that I use it so infre­quently, that I can never remember it.

    I hope other vis­itors see that there’s also some pretty good con­tent, as well as design going on here.

  4. Cheers Jeremy for your nice com­ment. Glad to be of service.

  5. I did that with my word­press data­base and wrecked it. It changed all the con­tent of every field to ‘post_content’ I don’t under­stand what went wrong. I don’t have a recent backup either, I have an out­dated one which will have to do. Sucks.

  6. I had no ideea about this ! Saves a lot of work. Thanks

  7. @Pet Supplies: I have no idea why this didn’t work for you. It works for me, everytime I use it and others have reported success.

    @Octav: You’re welcome.

  8. Thank you. I have had a hor­rible week as I lost my domain name through some weird situ­ation. After regis­tering some­thing new, I was not looking for­ward to fixing all the ref­er­ences to the former domain name (I use abso­lute paths). I found this post and within a second five years of blog­ging was updated.

    Thank you.

  9. @RDOwens: Wow that’s great, it’s really grat­i­fying to know that this little post helped you out like that. Thank you for let­ting me know. :-)

  10. […] no time could be found to run the SQL com­mand for search and replace that takes a split second (lit­er­ally) to […]

  11. this is my favourite bit of code ever, so handy!

  12. Thanks!

    Is there a way to search and replace through the entire database?

    Ed

  13. @Dave Newton: :-)

    @Ed: As far as I know there’s no single com­mand that’ll do the job in MySQL. However, one could def­in­itely script this with a little Perl per­haps. I’ve done this myself in the past.

  14. […] http://socialcmsbuzz.com/convert-import-a-drupal-6-based-website-to-wordpress-v27-20052009/ […]

  15. A sug­ges­tion:

    If you want to be on the safe side and not have some­thing nasty happen like what happen to Pet Supplies…

    First add a new column identical to the one you want to search and replace in. Second fill that column with the values from the primary one.

    Now run the search and replace on your new column and if it works, remove the primary one and rename your sec­ondary one to take its place.

    Piece of cake and you won’t risk losing data :)

    / Dario

  16. Thanks a lot! I thought I’d never find a solu­tion using MySQL query only. :) I almost decided to create a script using php and mysql for this problem.

    Thanks again!

  17. You’re very wel­come Marc.

    @Dario: Thanks for the sug­ges­tion. That’s good advice you’ve given. I appre­ciate it.

  18. Is this case sens­itive or an exact match? i.e. will “dog” be able to replace both “Dog” and “dog” and “doG” or will I have to handle each of those sep­ar­ately? Thanks for posting this by the way

  19. That’s a good ques­tion Jason and I’m really sur­prised that this hasn’t been asked before.

    The func­tion is case-sensitive.

  20. “Authored by: Ed • Comment Posted: Feb 22nd, 2009 •Permalink

    Thanks!

    Is there a way to search and replace through the entire database?

    Ed”

    If your data­base is small enough, you can export it to a text file, open it in text editor, do a find and replace, then update!

    In fact, while I like that com­mand and use it, doing it the way I describe ensure you have a BACKUP of your data­base.
    When you restore you can restore to a dif­ferent db, change the application’s config file or set­tings to point to the new db,etc. or you can simply backup the text file before you make your changes.

  21. @steve: Great com­ment. You’re abso­lutely right, search-and-replace on a text-file dump from the DB is a very effective way to accom­plish the same thing AND ensure a degree of safety. But this would only really be prac­tical on a smaller data­base as you have iden­ti­fied. Also, the user would have to take great care not to mess up the grammar and syntax of the SQL file oth­er­wise he/she would find it dif­fi­cult to import it back into the DB.

    Good tip though.

  22. @Ed I wrote a quick script you can upload to your server and do a find and replace across an entire data­base. Use at your own risk! There are no real safe­guards in this script. One string simply replaces another string.

    http://blog.irmsgeekwork.com/2009/phpmysql-full-database-search-and-replace

  23. @irms: Nice con­tri­bu­tion to the thread. Thank you.

  24. is there a way of repla­cing the exact words?
    For example: i have the word “Jo” ‘name‘table and i want to replace it with “hello”. But ‘name‘table i also have the word “John”. I haven’t tried it but i think this will result in repla­cing “Jo” with “hello”, but will also replace “John” with “hellohn”.

  25. Hi! I was surfing and found your blog post… nice! I love your blog. :) Cheers! Sandra. R.

  26. @sami: I can’t say for cer­tain without testing this, but IIRC this search-and-replace only does whole words.

    @sandrar: Thank you. :-)

  27. Anyway to replace one string with another in the same way you did that single word?

  28. Yes. I think this should work: update posts set con­tent = replace(content,‘replace this string’,‘with this one’);

    Or is that not what you meant?

  29. Thanks very much for posting up this simple SQL Query!

    I have a dis­cus­sion forum with about 40,000 posts — most of them con­taining lots of emoticons.

    The problem is that I changed my site’s domain name at one point and so all of those emoticons now ref­er­ence a domain name that no longer exists. That res­ulted in TONS of 404 errors and gaps in the posts that looked rather ugly.

    So, I needed to replace every instance of oldname.com with newname.com in the posts field without wrecking the posts them­selves (that would be a disaster).

    Your script enabled me to do that with ease and it updated 6,486 rows with the cor­rect domain name :-)

    I knew it could easily be done with SQL but I didn’t know how until I came across your site — thank you!

  30. That’s really cool Dave. Thank you for let­ting me know it worked for you. It’s great to hear these little suc­cess stories.

    All the best.

  31. This is great but how do I replace only one instance of the word instead of every?

  32. @Rims: Well surely, if you only have one instance of a word to replace, then your best option would be to edit that instance using whatever tool you nor­mally use for manip­u­lating your content?

    The search-and-replace mech­anism dis­cussed here is designed for making bulk changes.

Contribute to the Discussion:

The comment handler is Gravatar enabled.