1
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

1
  • Do you mean a column of telephone numbers? Commented Aug 10, 2010 at 17:15

4 Answers 4

1

Here is a simple MySQL regex that allows certain characters between groupings of numbers.

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

This matches your records but does not format them such that the misc. characters are removed, but you at least can find the records that match the number in question.

You could easily fix the formatting by changing this into an update statement.

Sign up to request clarification or add additional context in comments.

2 Comments

what if i want any number not just 1-999-999-9999 what if it can be any number, the way that you have it, it must be 999
I assumed you were trying to find all variations of a specific number, altering the query to find whatever number you were looking for. If you are trying to write a query that will return just the numbers, and strip out the extra characters, you'll need to do that similar to what ar suggested, by using replace or alter the data in code and update the database. Regexp only gives you a boolean result whether the pattern was matched or not.
1

MySQL doesn't have a regular expression replace function, but if you have a limited number of unwanted characters you could use a series of replace statements, eg.

select replace(replace(replace(telephone, '-', ''), '(', ''), ')', '') from ...

Comments

1

Use:

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

Reference:

Comments

0

You can also use a UDF (user defined function) to have a REGEX_REPLACE.

https://launchpad.net/mysql-udf-regexp

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.