Siva
Siva

Reputation: 1

How to pass the multiple search patterns to sql statement using WHERE IN clause

Am having the table with below data,

  +----------+---------+
  | Country  | Product |
  +----------+---------+
  | Poland   | Lyca    |
  | USA      | Lyca    |
  | UK       | GT      |
  | Spain    | GT      |
  | Swiss    | Lyca    |
  | Portugal | GT      |
  +----------+---------+

From the above table, I am trying to fetch the data using the query which is given below,

 Select Country,Product from tab where Country in ('%pai%','%U%')

Query was executing but i am getting the empty resuls. So, kindly confirm me, whether the above query is valid or not.

Upvotes: 0

Views: 221

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 415860

Get your patterns into a separate table:

PATTERNTABLE
SessionKey     Pattern  
----------     -------
abc123         %pai%
abc123         %U% 

and then join to it:

SELECT Country, Product
FROM Tab t
INNER JOIN PatternTable pt ON pt.SessionKey= @SessionKey 
     and t.Country LIKE pt.Pattern

The trick is in how that pattern table is populated.

Upvotes: 0

Gordon Linoff
Gordon Linoff

Reputation: 1269953

Use like and or:

Select Country,Product
from tab
where Country like '%pai%' or
      Country like '%U%';

Upvotes: 3

Related Questions