Search for text between delimiters in MySQL

前端 未结 11 2421
情歌与酒
情歌与酒 2021-02-08 03:39

I am trying to extract a certain part of a column that is between delimiters.

e.g. find foo in the following

test \'esf :foo: bar

So in the above I\'d wa

11条回答
  •  南笙
    南笙 (楼主)
    2021-02-08 03:52

    This one looks elegant to me. Strip all after n-th separator, rotate string, strip everything after 1. separator, rotate back.

    select
      reverse(
        substring_index(
          reverse(substring_index(str,separator,substrindex)),
          separator,
          1)
      );
    

    For example:

    select
      reverse(
        substring_index(
          reverse(substring_index('www.mysql.com','.',2)),
          '.',
          1
        )
      );
    

提交回复
热议问题