Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Combining "LIKE" and "IN" for SQL Server [duplicate]

Tags:

sql

sql-like

Is it possible to combine LIKE and IN in a SQL Server-Query?

So, that this query

SELECT * FROM table WHERE column LIKE IN ('Text%', 'Link%', 'Hello%', '%World%') 

Finds any of these possible matches:

Text, Textasd, Text hello, Link2, Linkomg, HelloWorld, ThatWorldBusiness 

etc...

like image 484
F.P Avatar asked Dec 08 '09 07:12

F.P


People also ask

Can you combine in and like in 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.

What is && in SQL?

Returns true if both expressions are true ; otherwise, false or NULL .

Can I use multiple like statements in SQL?

Not only LIKE, but you can also use multiple NOT LIKE conditions in SQL. You will get records of matching/non-matching characters with the LIKE – this you can achieve by percentile (%) wildcard character. Below use cases, help you know how to use single and multiple like conditions.

How do I match multiple values in SQL?

Note – Use of IN for matching multiple values i.e. TOYOTA and HONDA in the same column i.e. COMPANY. Syntax: SELECT * FROM TABLE_NAME WHERE COLUMN_NAME IN (MATCHING_VALUE1,MATCHING_VALUE2);


2 Answers

Effectively, the IN statement creates a series of OR statements... so

SELECT * FROM table WHERE column IN (1, 2, 3) 

Is effectively

SELECT * FROM table WHERE column = 1 OR column = 2 OR column = 3 

And sadly, that is the route you'll have to take with your LIKE statements

SELECT * FROM table WHERE column LIKE 'Text%' OR column LIKE 'Hello%' OR column LIKE 'That%' 
like image 161
Fenton Avatar answered Sep 19 '22 15:09

Fenton


I know this is old but I got a kind of working solution

SELECT Tbla.* FROM Tbla INNER JOIN Tblb ON Tblb.col1 Like '%'+Tbla.Col2+'%' 

You can expand it further with your where clause etc. I only answered this because this is what I was looking for and I had to figure out a way of doing it.

like image 38
lloydz1 Avatar answered Sep 20 '22 15:09

lloydz1