Skip to Main Content
  • Questions
  • Using REGEXP to find ten consecutive digits with possible separators

Breadcrumb

Question and Answer

Chris Saxon

Thanks for the question, SURESH.

Asked: November 07, 2018 - 10:58 pm UTC

Last updated: November 08, 2018 - 3:23 pm UTC

Version: 11.2

Viewed 10K+ times! This question is

You Asked

I HAVE THIS FREE FLOWING DATA FIELD IN A VARCHAR2 COLUMN THAT I AM CLEANING UP.


I WOULD LIKE TO GET THOSE VALUES THAT HAVE A TOTAL OF 10 DIGITS IN ALL AND MIGHT CONTAIN THESE CHARACTERS : . OR - OR / OR ( )

CREATE TABLE E ( PHONE VARCHAR2(50));


insert into e values('Tom phone number is 1234567890');
insert into e values('Tom phone number is 123.456.7890');
insert into e values('Tom phone number is 123-456-7890');
insert into e values('Tom phone number is 123/456/7890');
insert into e values('Tom phone number is (123)456-7890');
insert into e values('Tom phone number is');
insert into e values('Tom phone number is 11');
insert into e values('Tom Phone 1234567 is number is 123456789');
insert into e values('Tom Phone 123.4567 is number is 123.456.789');
insert into e values('Tom Phone is number is 123.456.789');

I AM LOOKING FOR THE RESULT THAT ONLY CONTAINS 10 DIGITS. SO ANYTHING THAT HAS 10 CONSEQUTIVE NUMBERS OR 10 NUMBERS WITH A - OR A . OR A ( ) IN IT.

THE RESULT TO LOOK LIKE:
1234567890
1234567890
1234567890
1234567890
1234567890

SO RECORDS FROM THE 1ST 5 ROWS AS OTHER RECORDS DO NOT SATISFY MY REQUIREMENT OF 10 DIGITS.

THANKS A LOT
SHARVU


and Chris said...

PLEASE STOP SHOUTING AT US!

Here's one approach you can take:

- Strip all the unwanted characters out of the input string first using replace, translate or a regex
- Check the remaining string contains a 10 digit sequence

For example:

select phone,
       translate ( phone, '1.-/()', '1' ) removed_chars, 
       regexp_substr ( 
         translate ( phone, '1.-/()', '1' ), 
         '[[:digit:]]{10}' 
       ) numbers 
from   e
where  regexp_like ( 
         translate ( phone, '1.-/()', '1' ), 
         '[[:digit:]]{10}' 
       );

PHONE                               REMOVED_CHARS                    NUMBERS      
Tom phone number is 1234567890      Tom phone number is 1234567890   1234567890   
Tom phone number is 123.456.7890    Tom phone number is 1234567890   1234567890   
Tom phone number is 123-456-7890    Tom phone number is 1234567890   1234567890   
Tom phone number is 123/456/7890    Tom phone number is 1234567890   1234567890   
Tom phone number is (123)456-7890   Tom phone number is 1234567890   1234567890   

Rating

  (1 rating)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Excellent use of REGEXP functions

sharvu, November 08, 2018 - 2:02 pm UTC

Apologize first for all the upper case in my text. I was having a challenging time getting to use the REGEXP functions. It is so nice to see how it got used to serve the result wanted.
Chris Saxon
November 08, 2018 - 3:23 pm UTC

No worries, glad this helped.

More to Explore

SQL

The Oracle documentation contains a complete SQL reference.