2

I want to search phone numbers from users table, it has million user records. The Users table will have phone numbers with or without country code.

Sample phone numbers in User table

+911111122222
3333344444
+15555566666
+917777788888
+19999900000

The input phone numbers which I will pass, it may or may not have the country code. I will input 100 to 1000 phone numbers

1111122222
+913333344444
+15555566666
7777788888
9999900000

I want to retrieve all records which matches phone numbers in despite of country code.

Right now I am using this,

Select * from users WHERE phone_numer IN ("1111122222","+913333344444", "+15555566666", "7777788888", "9999900000");

It doesn't return all the phone numbers, how should we handle this?

gvm
  • 1,153
  • 3
  • 12
  • 19

2 Answers2

1

You could possible just check if the number you want to test against is at the end of the number in the database, to do this, check out this question on how to use LIKE with multiple values.

WHERE phone_number LIKE '%1111122222'

will match both 1111122222 and +91111122222

Community
  • 1
  • 1
NDM
  • 6,731
  • 3
  • 39
  • 52
1
select *
from users
where 
CASE WHEN phone_number like '+%' THEN SUBSTRING(phone_number,4,len(phone_number))
ELSE phone_number IN (Pass phone numbers without code)

Example:

select *
from users
where 
CASE WHEN phone_number like '+%' THEN SUBSTRING(phone_number,4,length(phone_number))
ELSE phone_number END IN 
('1111122222','3333344444', '555566666', '7777788888', '9999900000')

SQL FIDDLE

Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71