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


Leave a response

Your response:

Categories