tags:

views:

120

answers:

5

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

+3  A: 

You need multiple LIKE clauses connected by OR.

SELECT * FROM table WHERE 
column LIKE 'Text%' OR 
column LIKE 'Link%' OR 
column LIKE 'Hello%' OR 
column LIKE '%World%' OR
Eric J.
NOOOOOOO. What if dozens of possible values? it's terrible...
Faruz
That's life? The question is about whether MS SQL supports that syntax. It does not. It's hard to speculate about a better solution without really knowing the problem at hand. Full text search may be an option as Mitch pointed out. It may also be desirable to write a stored procedure (in e.g. C#, which has more advanced text processing capabilities).
Eric J.
A: 

No, MSSQL doesn't allow such queries. You should use col LIKE '...' OR col LIKE '...' etc.

alygin
+1  A: 

No, you will have to use OR:

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

Have you looked at Full-Text Search?

Mitch Wheat
+2  A: 

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%'
Sohnee
+3  A: 

One other option would be to use something like 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
astander
@astander: That's what I thought of.
shahkalpesh