Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: sql - phone numbers
Gregory Caulton <gcaulton_at_sympatico.ca> wrote in message
news:3805FDE7.776A97F9_at_sympatico.ca...
> If I have a column with phone numbers in a variety of formats
> e.g. 555-555 5555
> (555)555-5555
> 5555555555
> and what it to insert into a column that is numeric i.e. I need to
> change all of them to a number, what is the best way, I seem to get
> 'invalid number' often when trying to mix to_number, translate and nvl
> together into my insert.
An interesting puzzle.
It can be done by using TRANSLATE() function twice to distill digits:
SQL> set numwidth 12
SQL> create table phone(no varchar2(20));
Table created.
SQL> insert into phone values ('123-456 7890');
1 row created.
SQL> insert into phone values ('(234)234-1234');
1 row created.
SQL> insert into phone values ('345/345/3456');
1 row created.
SQL> insert into phone values ('555-555-5555');
1 row created.
SQL> insert into phone values ('666x666x6666');
1 row created.
SQL> select * from phone;
NO
SQL> select to_number(translate(no,
2 '0'||translate(no, ' 0123456789', ' '), '0')) as no
3 from phone;
NO
![]() |
![]() |