How to get string after character oracle

后端 未结 2 1700
暗喜
暗喜 2020-12-10 12:50

I have VP3 - Art & Design and HS5 - Health & Social Care, I need to get string after \'-\' in Oracle. Can this be achieved using substring?

相关标签:
2条回答
  • 2020-12-10 13:01

    You can use regexp_substr():

    select regexp_substr(col, '[^-]+', 1, 2)
    

    If you want to remove an optional space, then you can use trim():

    select trim(leading ' ', regexp_substr(col, '[^-]+', 1, 2))
    

    The non-ovious parameters mean

    • 1 -- search from the first character of the source. 1 is the default, but you have to set it anyway to be able to provide the second parameter.
    • 2 -- take the second match as the result substring. the default would be 1.
    0 讨论(0)
  • 2020-12-10 13:06

    For a string operation as simple as this, I might just use the base INSTR() and SUBSTR() functions. In the query below, we take the substring of your column beginning at two positions after the hyphen.

    SELECT
        SUBSTR(col, INSTR(col, '-') + 2) AS subject
    FROM yourTable
    

    We could also use REGEXP_SUBSTR() here (see Gordon's answer), but it would be a bit more complex and the performance might not be as good as the above query.

    0 讨论(0)
提交回复
热议问题