47

Hey my first question on SO! Anywho...

Still relatively a newb at SQL so I think I might be missing something here. My question is I currently have a table full of phone numbers. I want to have a query where I search for phone numbers that are similar to a list I have. So for example, I want to find phone numbers that begin with '555123', '555321', and '555987'. I know normally if you have a list of numbers you could just do a query such as

SELECT * 
  FROM phonenumbers 
 WHERE number in ('5551234567', '5559876543', .... );

Is there a way to do this with like? Such as

SELECT * 
  FROM phonenumbers 
 WHERE number in like ('555123%', '555321%', '555987%'); //I know this doesn't actually work

Instead of have to do this individually

SELECT * 
  FROM phonenumbers 
 WHERE number like '555123%' 
    or number like '555321%' 
    or number like '555987%'; //Which does work but takes a long time

Or is there an easier to do this that I'm just missing? I'm using postgres, I don't know if there's any commands it has that would help out with that. Thanks!

9
  • 1
    Welcome to SO! I've retagged your question to clarify it relates to postgres Commented Feb 11, 2010 at 15:41
  • What version of Postgres? Sounds like a job for regexes: postgresql.org/docs/8.3/static/functions-matching.html Commented Feb 11, 2010 at 15:47
  • Which does work but takes a long time - unless you change the nature of the query, a different syntactic representation of the same thing will take as long. Is the column indexed? Commented Feb 11, 2010 at 15:52
  • Actually the column is indexed. I hadn't noticed that before. Oh, and I am using postgres 8.0. I have a large amount of numbers I am searching. Commented Feb 11, 2010 at 15:59
  • How long does your query take? How many records you have in your table? Commented Feb 11, 2010 at 16:00

6 Answers 6

81

You can use SIMILAR TO and separate the tags with | pipe '555123%|555321%|555987%'

eg:

SELECT * 
FROM phonenumbers 
WHERE number SIMILAR TO '555123%|555321%|555987%'
Sign up to request clarification or add additional context in comments.

Comments

46

Late to the party, but for posterity... You can also use a ANY(array expression)

SELECT * 
FROM phonenumbers 
WHERE number LIKE ANY(ARRAY['555123%', '555321%', '555987%'])

1 Comment

FWIW there's also a "LIKE ALL(...)" option stackoverflow.com/a/5742019/32453
7

Assuming all your numbers do not contain letters, and your numbers are always "prefixes" (ex: LIKE '123%'):

SELECT  number
FROM    (
        VALUES
        ('555123'),
        ('555321'),
        ('555000')
        ) prefixes (prefix)
JOIN    phonenumbers
ON      number >= prefix
        AND number < prefix || 'a'

This will use an index on phonenumbers, if any, so could be faster.

2 Comments

Do you have a blog or intentions to write a book about these? I would be among the buyers.
@Pentium10: explainextended.com The book is in progress, but this is a secret yet, please don't tell anybody.
4

You can also rely on POSIX regular expressions, see section 9.7.3 of the official documentation.

For example:

SELECT * FROM foobar WHERE name ~ '12345|34567';

It is important to note that your name field is of a string type.

1 Comment

Not sure why but this solution was the only one that worked for me.
3

I don't think so, but you could join phonenumbers on a table criteria containing the values you want to match on, i.e.

JOIN criteria ON phonenumbers.number LIKE criteria.phonenumbers

...probably not worth it for a small number of conditions, though

2 Comments

Actually I do have a lot more to search but thought I should keep it simple with just a few numbers.
or create temp table. Probably won't be any speedup from other options but an option whoa.
1

Maybe if your prefixes are all the same length then you can do where RIGHT(number) in ('123456', '234456', 'etc', 'etc')

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.