tags:

views:

28

answers:

2
SELECT telephone_number
FROM table
WHERE telephone_number REGEXP '^1[() -]*999[() -]*999[() -]*9999$';

how do i make so its valid for any number format and any number like

407-888-0909
1(408)998-7654
7776654433
876-7788

right now its only valid for 1-999-999-9999

A: 

It isn't very wise to store phone numbers in a database with spaces, dashes, parentheses, etc. The most efficient way is to truncate all that garbage to a simple 10 digit number. That way you can actually store the number in an INTEGER based column instead of a VARCHAR.

Zane Edward Dockery
I am not doing the insert so i just need to pull out
Matt
+2  A: 

Use:

SELECT telephone_number
  FROM table
 WHERE telephone_number REGEXP '^1[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{4}$';

Reference:

OMG Ponies
what if i have a value 6309991234 but in the database its in (630)9991234 can i add a REGEXP '^1[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{3}[() -]*[[:digit:]]{4}$'="6309991234"
Matt
@John: The brackets support "m,n", so you could use {3,7} to capture "9991234". I'm still messing with the functionality to get optional character support working...
OMG Ponies