Skip to content
Advertisement

Oracle SQL REGEXP to fetch the last numeric value in a string

I have the below string value in a column and I need to extract the last numeric value. I have used SUBSTR(ColumnA, -1, 1), but this is extracting only the last digit.

ColumnA
12_23_AB245-F5
66_78_HJ378-G5567
55_16_GC761-B99898

Below is the expected result

ColumnA
5
5567
99898

Advertisement

Answer

Use REGEXP_SUBSTR here with the pattern [0-9]+$:

SELECT ColumnA, REGEXP_SUBSTR(ColumnA, '[0-9]+$') AS last_digits
FROM yourTable;

screen capture from demo link below

Demo

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement