Combining "LIKE" and "IN" for SQL Server

F.P picture F.P · Dec 8, 2009 · Viewed 779.9k times · Source

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...

Answer

Fenton picture Fenton · Dec 8, 2009

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%'