Sunday, March 28, 2021

sql query to replace a substring

We needed to replace some Windows paths in a MySQL database. Following 
https://www.mysqltutorial.org/mysql-string-replace-function.aspx/

update tablename set tablename.MediaSource = REPLACE(tablename.MediaSource,
        m4a,
        mp3)
WHERE
    tablename.MediaSource like '%Dashboard%1%1%2%1%2.m4a';

and if we wanted to be more specific, the '\' in the Windows paths need to be escaped as four backslashes - following https://stackoverflow.com/questions/14926386/how-to-search-for-slash-in-mysql-and-why-escaping-not-required-for-wher - 

select * from tablename where tablename.MediaSource like '%Dashboard%1%1%2\\\\1\\\\2.m4a';

And the syntax for a count statement was like

select count(*) from (select * from tablename where tablename.MediaSource like '%Dashboard%m4a') myqueryname;

No comments:

Post a Comment