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
- Dozens more answers to tough SQL questions from Rudy Limeback.
- The Best SQL Web Links: tips, tutorials, scripts, and more.
- Have an SQL tip to offer your fellow DBAs and developers? The best tips submitted will receive a cool prize. Submit your tip today!
- Ask your technical SQL questions -- or help out your peers by answering them -- in our live discussion forums.
- Ask the Experts yourself: Our SQL, database design, SQL Server, DB2, object-oriented and data warehousing gurus are waiting to answer your toughest questions.
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.