views:

18

answers:

2

I have a field in my database where users have saved free-form telephone numbers.

As a result, the data has all sorts of different formatting: (area) nnn-nnnn, area-nnn-nnnn, area.nnn.nnnn, etc.

I would like to strip out all the non-numeric characters and just store the digits, but I can't find a simple way to do this. Is it possible without using one REPLACE for each char?

+1  A: 

You can use REGEXP_REPLACE:

SELECT REGEXP_REPLACE('+34 (947) 123 456 ext. 2013', '[^0-9]+', '')
FROM DUAL

This example returns 349471234562013

Álvaro G. Vicario
+1  A: 

For older versions of Oracle that don't support regular expressions:

select translate (phone_no,'0'||translate (phone_no,'x0123456789','x'),'0')
from mytable;

The inner translate gets all the non-digit characters from the phone number, and the outer translate then removes them from the phone number.

Tony Andrews