Normalizing a field using a single UPDATE in MySQL
-
25-05-2021 - |
Question
Is there any way to perform an update for normalizing a field using a single query?
Example:
UPDATE person SET name = REPLACE(name, 'á', 'a');
UPDATE person SET name = REPLACE(name, 'é', 'e');
UPDATE person SET name = REPLACE(name, 'í', 'i');
UPDATE person SET name = REPLACE(name, 'ó', 'o');
UPDATE person SET name = REPLACE(name, 'ú', 'u');
Solution
You can chain the replace calls, so it can be done in a single query:
UPDATE person SET name = REPLACE(REPLACE(REPLACE(name, 'á', 'a'), 'é', 'e'), 'í', 'i')
But this quickly becomes an unmaintainable mess. If you're simply trying to replace accented characters with their unaccented equivalents, maybe a character set change would be of more use.
OTHER TIPS
Replace like this you all are talking about become mess if u want to replace all alphabets. You can make one table with two colums such as 1st column consist 'á' and second 'a' similarly for whole. and then make a procedure to fetch each row of new table and then done the replace.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow