Coder Perfect

For SQL Server, combining “LIKE” and “IN” [duplicate]

Problem

In a SQL Server query, is it feasible to mix LIKE and IN?

As a result, this inquiry

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

Any of the following probable matches are found:

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

etc…

Asked by F.P

Solution #1

The IN statement effectively creates a sequence of OR expressions…

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

Is effectively

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

Regrettably, it is the path your LIKE statements will have to travel.

SELECT * FROM table
WHERE column LIKE 'Text%' OR column LIKE 'Hello%' OR column LIKE 'That%'

Answered by Fenton

Solution #2

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 may make it more complex by adding a where clause, for example. I simply responded because it was exactly what I needed and I needed to find a way to get it.

Answered by lloydz1

Solution #3

Another alternative is to use something similar to this.

SELECT  * 
FROM    table t INNER JOIN
        (
            SELECT  'Text%' Col
            UNION SELECT 'Link%'
            UNION SELECT 'Hello%'
            UNION SELECT '%World%'
        ) List ON t.COLUMN LIKE List.Col

Answered by Adriaan Stander

Solution #4

No, you’ll have to join your LIKE expressions using OR:

SELECT 
   * 
FROM 
   table
WHERE 
   column LIKE 'Text%' OR 
   column LIKE 'Link%' OR 
   column LIKE 'Hello%' OR
   column LIKE '%World%'

Have you tried Full-Text Search yet?

Answered by Mitch Wheat

Solution #5

Multiple LIKE clauses must be joined by OR.

SELECT * FROM table WHERE 
column LIKE 'Text%' OR 
column LIKE 'Link%' OR 
column LIKE 'Hello%' OR 
column LIKE '%World%' OR 

Answered by Eric J.

Post is based on https://stackoverflow.com/questions/1865353/combining-like-and-in-for-sql-server