I have this string in some MariaDB Column (let’s called it MY_COL):
{"libelle": "comu00e9dien"}
that i’d like to replace it with
{"libelle": "acteur"}
Using this STO post, I figured out how to select rows containing this string:
SELECT MY_COL
FROM MY_TABLE
WHERE MY_COL LIKE '%{"libelle": "com\\u00e9dien%'
But, when i used this "over-sized escaping" technic in an UPDATE Statement, it doesn’t work
UPDATE MY_TABLE
SET MY_COL = REPLACE(MY_COL, '{"libelle": "com\\\u00e9dien"', '{"libelle": "acteur"')
Nevertheless, I noticed that this SELECT statement works well:
SELECT '{"libelle": "com\\\u00e9dien"' As `Column_Before`,
REPLACE('{"libelle": "com\\\u00e9dien"', '{"libelle": "com\\\u00e9dien"', '{"libelle": "acteur"') As `Column_After`;
It seems that refering to the Column_Name in the REPLACE statement leads to this issue but neither i don’t know why nor i don’t find how to prevent that…
Last info: the table, the column and the database use UTF8MB4 encoding with utf8mb4_general_ci collation.
2
Answers
backslash
is an escape character in bothJSON
andSQL
strings, so it must be escaped, try this >>Method 1:
Method 2:
Method 3:
UPDATE MY_TABLE
Method 4:
You should use the proper JSON functions
If you have an array of such objects you can unnest them in a subquery, change what needs to be changed and reaggregate.