From string, extract a word starting with a keyword (SQL or any other methods?)

Let's say I have a string of random characters, and I want to extract only the string that starts after a specific keyword (including the keyword). How could I do so using MySQL or MariaDB? ..............

String example:

axdsSSFddlwie ====> SSFddlwie

aldfklafnlanaSSFiiie ====> SSFiiie

iiiSSFnnnn ====> SSFnnnn

Thanks for the help!

2 answers

  • answered 2020-06-02 10:23 Tim Biegeleisen

    Using the base string functions we can try:

    SELECT SUBSTRING(col, INSTR('SSF', col))
    FROM yourTable;
    

    If you are using MySQL 8+, then REGEXP_SUBSTR is another option:

    SELECT REGEXP_SUBSTR(col, 'SSF.*')
    FROM yourTable;
    

  • answered 2020-06-02 10:24 Gordon Linoff

    You can use substring_index():

    select concat('SSF', substring_index(col, 'SSF', -1))