0

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')))
MT0
  • 143,790
  • 11
  • 59
  • 117
  • are you sure this is mysql? mysql does not have a LEN() function. if you meant LENGTH, mixing LENGTH and SUBSTRING doesn't work well with non-ascii data; you should use CHAR_LENGTH instead – ysth Sep 23 '22 at 22:56

2 Answers2

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

MT0
  • 143,790
  • 11
  • 59
  • 117
0

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

nbk
  • 45,398
  • 8
  • 30
  • 47