Posted by: R Manimaran | June 30, 2008

SQL Server LIKE Statement & IN Statement

We use the IN Statement in the SQL Queries Here are the some examples of using them using the methods available in SQL Server

Declare @P varchar(50)
Set @P=’2,5,7,9′
SELECT LevelID,Position FROM SampleOrg WHERE
CHARINDEX(‘,’+Cast(LevelID as varchar(10))+’,’, ‘,’+@P+’,’)>0;
Select LevelId,position FROM SampleOrg Where
PATINDEX(‘%,’+Cast(LevelID as varchar(10))+’,%’,’,’+@P+’,’)>0;
–Using LIKE
Select LevelId,Position FROM SampleOrg where
‘,’+@P+’,’ LIKE ‘%,’+Cast(LevelID as varchar(10))+’,%’;

output will be like this

LevelID Position


2 Senioor Director

5 Product Development Manager

7 QA Lead

9 Developers


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: