0
Table A
ID     ID_Descr
1     'DUP 8002061286'
2     'DUP 8002082667  '
3     ' 8002082669 DUP'

I would like to extract the string from the ID_Descr field with the following conditions:

  1. String always starts with 8
  2. String length is always 10 digits

This means stripping everything to the right and left of the string (eg. '8002082669'). How can I achieve this? Using REGEXP_SUBSTR?

I am using Oracle 11g.

Thanks!

Heisenberg
  • 267
  • 3
  • 6
  • 15

2 Answers2

3

Although you could use regexp_substr() for this, I would take a different approach. I would just look for the '8' using instr() and then take the next 10 characters:

select substr(id_descr, instr(id_descr, '8'), 10)

This seems like the simplest solution.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
2

You could use REGEXP_SUBSTR() but a regex is an expensive operation so you would be much better off using SUBSTR() and INSTR():

SELECT SUBSTR(ID_Descr, INSTR(ID_Descr, '8'), 10) FROM tableA;

If you really wanted to use REGEXP_SUBSTR() you could do it as follows:

SELECT REGEXP_SUBSTR(ID_Descr, '8.{9}') FROM tableA;

This would get 8 plus the following 9 characters (. being the wildcard).

Now if you wanted to match only digits, then REGEXP_SUBSTR() would probably be your best bet:

SELECT REGEXP_SUBSTR(ID_Descr, '8[0-9]{9}') FROM tableA;
David Faber
  • 12,277
  • 2
  • 29
  • 40