Problem solve Get help with specific problems with your technologies, process and projects.

Can one group LIKEs together?

Can one group LIKEs together, such as:

select * from A 
 where B like ('ccc', 'ddd', 'eee')

No. You have to "distribute" the LIKE operator over the individual items in your list. This is not just a syntax requirement, it is also good coding practice, because it allows you to concentrate on exactly what you mean.

First, you will need to decide whether you want the conditions connected with ORs or ANDs. Second, if you want all three strings present, do the strings have to exist in the given sequence? Finally, you should really use wildcard characters; otherwise, each LIKE in effect becomes a strict equality, and then only the OR version even makes sense.

Find rows where B contains any one (or two, or three) of the three strings:

select * 
  from A 
 where B like '%ccc%'
    or B like '%ddd%'
    or B like '%eee%'

Find rows where B contains all three strings, in any order:

select * 
  from A 
 where B like '%ccc%'
   and B like '%ddd%'
   and B like '%eee%'

Find rows where B contains all three strings, in the given order:

select * 
  from A 
 where B like '%ccc%ddd%eee%'

For More Information

This was last published in December 2003

Dig Deeper on Oracle and SQL

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.