I have strings values such as the following in a column:
|333|,|331|
I want to perform a balanced string replacement as follows:
xxTM_333_TMxx,xxTM_331_TMxx
I have tried to do this with the REPLACE and CONCAT functions but didn't get the desired output.
For example:
SELECT REPLACE('|333|,|331|','|','xxTM');
This replaces one of the | symbols correctly in each case, but not its matched (balanced) counterpart.
How can I achieve this result in MySQL?
|means something different for every alternative match. You can do this with a regular expression replace, which MySQL doesn't support natively. Do it in your application.select concat(trim(TRAILING 'xxTM' FROM replace(replace('|333|,|331|,|234|','|','xxTM'),'xxTM,xxTM','TMxx,xxTM')),'TMxx');will outputxxTM333TMxx,xxTM331TMxx,xxTM234TMxx