Friday, January 16, 2009

MySQL Search and Replace

I recently had to update some information in MySQL. Yes, I'm sure you're thinking, "yea, so...that's easy!". But, the way the data was entered in the column was more like an entire paragraph! The information I needed to change was only a last name in the paragraph. So, obviously I didn't want to type out the entire paragraph again with my edits just so i can UPDATE the table. So instead, MySQL has a nice replace function built in. Yay!

The code will look as follows:
UPDATE your_table set your_column=replace(your_column,'Smith','Jones');

And now your_column will replace all occurrences of "Smith" with "Jones".

Cheers!

No comments: