I have a data set like this (see below) and I try to extract digits which are in form {variable_number_of_digits}{hyphen}{only_one_digit}:
with mcte as (
select 'ILLD/ELKJS/00000000/ELKJS/FHSH' as addr from dual
union all
select 'ILLD/EFECTE/0116988-7-002/ADFA/ADFG' as addr from dual
union all
select 'IIODK/1573230-0/2216755-7/' as addr from dual
union all
select 'IIODK/1573230-0/2216755-700/WRITE' as addr from dual
)
select addr,
REGEXP_SUBSTR(addr,'(\/)([0-9-]+)',1,1,NULL,2) AS num1,
REGEXP_SUBSTR(addr,'(\/)([^\/]+\/)([0-9\-]+)',1,1,NULL,3) num2
from mcte
;
I'm not getting a correct results set which should be the following
+-------------------------------------+-----------+-----------+
| ADDR | NUM1 | NUM2 |
+-------------------------------------+-----------+-----------+
| ILLD/ELKJS/00000000/ELKJS/FHSH | NULL | NULL |
| ILLD/EFECTE/0116988-7-002/ADFA/ADFG | NULL | NULL |
| IIODK/1573230-0/2216755-7/ | 1573230-0 | 2216755-7 |
| IIODK/1573230-0/2216755-700/WRITE | 1573230-0 | NULL |
+-------------------------------------+-----------+-----------+
How to achieve this?
If you want to get the results from the second and third
/
delimited groups then:Output:
Update:
If you just want the first and second pattern that match and do not care where they are in the string then:
Outputs:
Combining the delimiter split query with
REGEXP_LIKE
and pivot-ing the result you get this query working for up to 6 numbers. You will need to update thecols
subquery and tehpivot
list to be able to process more numbers per record. (Unfortunately this can't be done general in a static SQL).this gives a result
To match numbers in this format you should do something like this.
Regex:
\/\d+-\d
Regex101 Demo