skip to Main Content

For example ‘ABC-12345-6789-10’ I want to return "10" as these are all the characters after the last "-" dash.

I have tried this but I only get a zero at the end:

SUBSTRING('ABC-12345-6789-10',len('ABC-12345-6789-10'),LEN(CHARINDEX('-', 'ABC-12345-6789-10')))

2

Answers


  1. Oracle does not support the functions:

    • SUBSTRING – you want SUBSTR instead.
    • LEN – you want LENGTH instead.
    • CHARINDEX – you want INSTR instead.

    You want:

    SELECT value,
           SUBSTR(value, INSTR(value, '-', -1) + 1) AS last_term
    FROM   (SELECT 'ABC-12345-6789-10' AS value FROM DUAL)
    

    Which outputs:

    VALUE LAST_TERM
    ABC-12345-6789-10 10

    fiddle

    Login or Signup to reply.
  2. For MySQL it is easier. there you have SUBSTRING_INDEX

    SELECT SUBSTRING_INDEX('ABC-12345-6789-10','-' ,-1)
    
    SUBSTRING_INDEX(‘ABC-12345-6789-10′,’-‘ ,-1)
    10

    fiddle

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search