MySQL substring replace, with simple query builder
filed under: Web Development / MySQL
I hate the MySQL docs, for whatever reason, they always seem so much less intuitive than the PHP docs. Granted, I don't work with MySQL religiously, but I find I often need to search for the same thing again and again.
One of the things I use most lately is a substring replace, for example, all my meta keywords that had the term "snews"... I wanted them to be "sNews", note the caps.
I wanted a simple, quick way to globally replace all instances of "snews" with "sNews". Simple enough, it's not hard to do once you figure it out, but it's definitely not easy to figure out from the online docs for MySQL's string functions.
Essentially, here is the command you need to issue. You need the database table, field, the text you're searching for, and the text you want to change it too. Like this;
UPDATE [table]
SET [field] = REPLACE ([field],'[text_to_find]','[text_to_replace]');
Now I know I'm going to use this one quite a bit, and I also needed an excuse to do a little jQuery :), so I wrote up a simple query builder for the substring replace. Just fill in the fields and click "Generate Query" and you're good to go.
*TIP: type naturally, the script will escape apostrophes for you.
UPDATE [table] SET [field] = REPLACE ([field],'[text_to_find]','[text_to_replace]');
If you want to restrict the replacement, you simply add a WHERE clause, WHERE articleid = '15', etc. Any questions, feel free to leave a comment.
3 comments
Add a new comment »Categories
Recent Entries
Recent Comments
- Dede (I checked it today in a shop. GT2 had some troubles with six ...)
- Matt (Bintang, You need to re-direct the url, try ...)
- jesth (Ohh.. why didn't I think of that, thanks alot.)
- Matt (Dede, I don't have Gran Turismo 2, any of the 2nd+ generation ...)
- Matt (Jesth, Just change the if condition, instead of looking for ...)
- jesth (Hi (again) Was wondering, is it possible to make it ...)
- Bintang Sembilan (Matt, thanks for your modd. I have apply it to my ...)
- Dede (Hello there. Can you check something for me? I want to buy ...)
- Matt (I think it's a driver issue Terrence, or it was a driver issue. ...)
Popular Entries
- Light-weight related articles mod for sNews 1.7 (4.5/5)
- Image / math hybrid captcha version 2, vastly improved (4.42/5)
- 1024x600 netbook wallpapers of Evangeline Lilly (4.4/5)
- Compact archives for sNews 1.7 (4.4/5)
- sNews Ajax Polls mod now available (4.38/5)
- Pretty date and comments bars in sNews CMS (4.35/5)
- Page caching mod for sNews 1.7 (4.33/5)
- Gravatar mod for sNews 1.7 (4.29/5)
- An improved tag cloud for sNews 1.7 (4.29/5)
Apr 16th, 2010 at 9:31 am
Thanks. I got solution from this article.
Apr 20th, 2010 at 11:01 pm
thanks...how about if i have a data like asd040404 and i want to replace the last for digit '0404' to 0504..how should i do it?
Apr 21st, 2010 at 9:03 am
Hi Lim,
Well, if you know it's going to be asd040404, then you'd just use;
UPDATE [table] SET [field] = REPLACE ([field],'asd040404','asd040504');If the beginning of the string is different and you know for a fact that there will be a space (or other character) after the last "0404", you could add the white space.
Something like;
UPDATE [table] SET [field] = REPLACE ([field],'0404 ','0504 ');Otherwise, you'll need to do some deeper pattern matching, and I don't believe MySQL can currently capture matches with REGEXP.
At that point I would probably write a small PHP script to select all the rows containing the basic pattern, create the new data with PHP regular expressions and then do an update on each matching row.
Do you have any more examples of the pattern you're looking for?