Click here to Skip to main content
15,616,520 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I need to write a stored procedure in MySQL that loops through all rows in a table (the column types are TEXT and VARCHAR) and replaces certain characters in the column values with another character. For example, a certain column contains the text "ababa" and I want to change it to "acaca".

I can't quite figure out the part about replacing a character in a string with another and could use some help with this!

Thank you very much!

1 solution

You need to sort out if you're wanting to replace a char or a string. The letter 'b' is different than the word "ababa", so you should be a little more clear there: are you wanting to replace 'b' with 'c' or "ababa" with "acaca"?

Regardless, you're going to want to use the UPDATE and REPLACE commands.

UPDATE your_table SET your_field=(REPLACE(your_field,'old-text','new-text')

old-text is (are) the character(s) you wish to replace, and new-text is (are) the character(s) you'll replace them with.

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900