Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

MySQL – Replace string when it appears

The column ‘PrizeMoneyBreakDown’ includes a number of strings seperated by a semi-colon. I am trying to remove the strings ‘total value’, ‘trophy total value’, and ‘welfare fund’ from the data. These strings only appear sometimes in the data so it is not as simple as just removing the last three strings. I need to write a query that removes the strings IF they appear.

Example of data:

1st,5285;2nd,1680;3rd,885;4th,550;5th,350;6th,350;7th,350;8th,350;total_value,10000;welfare_fund,200;trophy_total_value,150;

Desired output of data:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

1st,5285;2nd,1680;3rd,885;4th,550;5th,350;6th,350;7th,350;8th,350

Current code (only removes the words ‘total value’ etc – does not remove prize money associated with string):

SELECT PrizeMoneyBreakDown,
  REPLACE(REPLACE(REPLACE(PrizeMoneyBreakDown,'total_value',""),'welfare_fund',""),'trophy_total_value',"") as new
FROM race2;

>Solution :

On MySQL 8+, we can use REGEXP_REPLACE:

SELECT PrizeMoneyBreakDown,
       REGEXP_REPLACE(PrizeMoneyBreakDown,
                      '(total_value|welfare_fund|trophy_total_value),\\d+;',
                      '') AS NewPrizeMoneyBreakDown
FROM race2;

If you want to update the actual column then use:

UPDATE race2
SET PrizeMoneyBreakDown = REGEXP_REPLACE(
    PrizeMoneyBreakDown,
    '(total_value|welfare_fund|trophy_total_value),\\d+;',
    '')
WHERE PrizeMoneyBreakDown REGEXP '(total_value|welfare_fund|trophy_total_value),\\d+;';
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading