0

There are strings in table

+1-123-456-7890 
11234567890
+1 1234567890
1777555999

I have variable $phoneNumber. I want to pass variable in sql query to get matching records. example: $phoneNumber holds the value 1234567890 and using same variable I want to fetch following records in one query

+1-123-456-7890 
11234567890
+1 1234567890

I want to select both records in one query. how can I match data (11234567890) with above saved numbers?

5
  • what MySQL Regex pattern have you tried? Commented Sep 27, 2018 at 7:28
  • 2
    REPLACE(REPLACE(REPLACE(telephone, ' ', ''), '+', ''), '-', '') LIKE $telephone Commented Sep 27, 2018 at 7:29
  • What version of MySQL are you using? Commented Sep 27, 2018 at 8:33
  • Now your question is not clear. Which of the three sample numbers should be matched here? After removing +, -, and spaces, only the middle number is an exact match. Commented Sep 27, 2018 at 9:55
  • +1-123-456-7890 11234567890 +1 1234567890 These numbers should be returned every time when $phoneNumber value is any of the following +1-123-456-7890 11234567890 +1 1234567890 all number should be treated as same because they have same number if ' ', '-', '+' characters not considered. Commented Sep 27, 2018 at 10:04

5 Answers 5

2

We can do this following the replacement logic already suggested here combined with regular expressions, to match only the phone numbers with the format you want to target:

WITH yourTable AS (
    SELECT '+1-123-456-7890' AS phone UNION ALL
    SELECT '+1 1234567890' UNION ALL
    SELECT '+6512345678'
)

SELECT
    phone,
    REPLACE(REPLACE(REPLACE(phone, ' ', ''), '+', ''), '-', '') AS common_phone
FROM yourTable
WHERE phone REGEXP '\\+[0-9][[:space:]-][0-9]{3}-?[0-9]{3}-?[0-9]{4}';

enter image description here

Demo

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

5 Comments

Thanks for the Reply. I am looking for something like this. select phone from myTable where phone like '{some regex pattern which can filter with the value of variable $phoneNumber}'; Here $phoneNumber can be any pattern with '+' and '-' or just number.
What issues would there be in adapting my raw MySQL query to PHP code?
How can I compare a variable with the table data to filter the results? above query will return results only matching to pattern. It will ignore number like this '11234567890' WITH yourTable AS ( SELECT '11234567890' AS phone UNION ALL SELECT '+1 1234567890' UNION ALL SELECT '+6512345678' ) SELECT phone, REPLACE(REPLACE(REPLACE(phone, ' ', ''), '+', ''), '-', '') AS common_phone FROM yourTable WHERE phone REGEXP '\\+[0-9][[:space:]-][0-9]{3}-?[0-9]{3}-?[0-9]{4}';
I don't understand your comment. If your requirements have changed, or I missed something, then edit your question and add more information.
Sorry for the confusion. I have edited the question to explain the problem.
0

Try this !

Regex :

 [\+]?[0-9](?:[-| ])?\d{3}[-]?\d{3}[-]?\d{4}

Verify through regxe101:

enter image description here

1 Comment

Your regex pattern can't be used in a MySQL query.
0

If both $phonenumber and your string column could (potentially) both have the troublesome characters, this is hard to solve with regular expressions.

where replace(replace(replace($phonenumber, ' ', ''), '-', ''), '+') = replace(replace(replace(phonecol, ' ', ''), '-', ''), '+')

1 Comment

Thank you for the reply, I solved this with something similar query. I removed special characters from variable $phoneNumber and compared it with the string result of the replace. SELECT phone FROM myTable where REPLACE(REPLACE(REPLACE(phone, ' ', ''), '+', ''), '-', '') = $phoneNumber;
0

I solved this with using replace() in query. I removed special characters from variable $phoneNumber (not through sql but before passing varable to sql) and compared it with the string result of the replace. SELECT phone FROM myTable where REPLACE(REPLACE(REPLACE(phone, ' ', ''), '+', ''), '-', '') = $phoneNumber;

Comments

0

Rethink the overall design.

Cleanse phone numbers before putting them into the table. Then that SELECT, and the other SELECTs that are yet to be written, will be simple.

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.