394

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

1

5 Answers 5

412

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%'
9
  • 8
    is this the only available syntax?
    – shaffooo
    Apr 5, 2015 at 15:38
  • Just a note: the last query also works in oracle(11g)
    – Abrar
    Feb 2, 2016 at 15:51
  • I was looking for regex example inside the like but this will do for now ! Mar 24, 2016 at 9:29
  • You could use build dynamic SQL statement, stacking the OR's. To improve security, should use parameter binding stackoverflow.com/questions/19943202/…
    – oglester
    Nov 30, 2017 at 14:21
  • Last query will not work if IN clause statement returns indefinite number of values. Jan 22, 2019 at 12:30
317

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.

16
  • 13
    @lloydz1 this isn't the original question though which has a column of values created on the fly. You are doing this on two tables.
    – Riz
    Dec 16, 2015 at 18:06
  • 21
    Does not accurately address the question. This works with a standard column from another table, not a manually curated list of words.
    – Dale Kube
    Aug 18, 2017 at 14:15
  • 5
    This can get duplicate results if col1 match the condition for diferent like values
    – FRL
    Oct 1, 2018 at 10:52
  • 7
    Of course it answers the question - you can do with a table everything you can do with a list, but not vice versa hence THIS is the general solution Nov 14, 2018 at 11:44
  • 4
    I agree with @GerasimosR. Its trivial to use to an in line table statement with a list (SELECT * FROM VALUE ('%patern1%', pat%ern%2') AS TableAlias(Col1)) instead of a table. Feb 12, 2019 at 23:04
135

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
5
  • @astander: That's what I thought of. Dec 8, 2009 at 8:11
  • 12
    This is the closest to the spirit of the question, so +1 Apr 22, 2014 at 13:05
  • Yes this is the answer that i was looking for Thank you May 29, 2015 at 17:05
  • 12
    The problem with this solution is if the text column contains text that would find more than one match. For example if your text was 'Hello World' it would find two matches and create an extra row in the results. The first line should be SELECT DISTINCT t.* to avoid this happening. Nov 16, 2015 at 9:55
  • 4
    How is this better, faster or simpler than using multiple LIKE conditions connected with OR?
    – csar
    Jun 15, 2020 at 7:03
24

No, you will have to use OR to combine your LIKE statements:

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?

0
7

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.