Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a combination of "LIKE" and "IN" in SQL?

In SQL I (sadly) often have to use "LIKE" conditions due to databases that violate nearly every rule of normalization. I can't change that right now. But that's irrelevant to the question.

Further, I often use conditions like WHERE something in (1,1,2,3,5,8,13,21) for better readability and flexibility of my SQL statements.

Is there any possible way to combine these two things without writing complicated sub-selects?

I want something as easy as WHERE something LIKE ('bla%', '%foo%', 'batz%') instead of this:

WHERE something LIKE 'bla%' OR something LIKE '%foo%' OR something LIKE 'batz%' 

I'm working with SQl Server and Oracle here but I'm interested if this is possible in any RDBMS at all.

like image 422
selfawaresoup Avatar asked Jun 10 '10 13:06

selfawaresoup


People also ask

Can I combine in and like in SQL?

Is there as way to combine the "in" and "like" operators in Oracle SQL? Answer: There is no direct was to combine a like with an IN statement. However Oracle does support several alternative clauses: CONTAINS clause: the contains clause within context indexes.

How do you use the like and in operators in an SQL query?

The LIKE Operator in SQL is used to extract records where a particular pattern is present. In a WHERE clause, the LIKE operator is used to look for a certain pattern in a column. In SQL, it has two wildcard characters, such as: Percentage symbol (%): It is a substitution for zero, one, or more characters.

How use like with in clause in SQL?

The LIKE command is used in a WHERE clause to search for a specified pattern in a column. You can use two wildcards with LIKE : % - Represents zero, one, or multiple characters. _ - Represents a single character (MS Access uses a question mark (?)


2 Answers

There is no combination of LIKE & IN in SQL, much less in TSQL (SQL Server) or PLSQL (Oracle). Part of the reason for that is because Full Text Search (FTS) is the recommended alternative.

Both Oracle and SQL Server FTS implementations support the CONTAINS keyword, but the syntax is still slightly different:

Oracle:

WHERE CONTAINS(t.something, 'bla OR foo OR batz', 1) > 0 

SQL Server:

WHERE CONTAINS(t.something, '"bla*" OR "foo*" OR "batz*"') 

The column you are querying must be full-text indexed.

Reference:

  • Building Full-Text Search Applications with Oracle Text
  • Understanding SQL Server Full-Text
like image 131
OMG Ponies Avatar answered Oct 18 '22 16:10

OMG Ponies


If you want to make your statement easily readable, then you can use REGEXP_LIKE (available from Oracle version 10 onwards).

An example table:

SQL> create table mytable (something)   2  as   3  select 'blabla' from dual union all   4  select 'notbla' from dual union all   5  select 'ofooof' from dual union all   6  select 'ofofof' from dual union all   7  select 'batzzz' from dual   8  /  Table created. 

The original syntax:

SQL> select something   2    from mytable   3   where something like 'bla%'   4      or something like '%foo%'   5      or something like 'batz%'   6  /  SOMETH ------ blabla ofooof batzzz  3 rows selected. 

And a simple looking query with REGEXP_LIKE

SQL> select something   2    from mytable   3   where regexp_like (something,'^bla|foo|^batz')   4  /  SOMETH ------ blabla ofooof batzzz  3 rows selected. 

BUT ...

I would not recommend it myself due to the not-so-good performance. I'd stick with the several LIKE predicates. So the examples were just for fun.

like image 20
Rob van Wijk Avatar answered Oct 18 '22 16:10

Rob van Wijk