0

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.

Siva
  • 1
  • 2
  • Your `IN` condition will compare the country value to the listed string, that the reason of your empty result (the `%` character is used as substitution only for `LIKE` condition) – Joël Salamin Sep 23 '14 at 13:29
  • possible duplicate of http://stackoverflow.com/questions/1865353/combining-like-and-in-for-sql-server – Aramillo Sep 23 '14 at 13:39

2 Answers2

3

Use like and or:

Select Country,Product
from tab
where Country like '%pai%' or
      Country like '%U%';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

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.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794