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′
–Using CHARINDEX
SELECT LevelID,Position FROM SampleOrg WHERE
CHARINDEX(‘,’+Cast(LevelID as varchar(10))+’,’, ‘,’+@P+’,’)>0;
–Using PATINDEX
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

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

Categories

%d bloggers like this: