Home » » using variable in "IN" clause in sql

using variable in "IN" clause in sql

Author : Sandeep Mittal on Saturday, March 23, 2013 | 3/23/2013 04:11:00 PM

I have seen most of the developers facing the problem while using variable in "IN" clause in Sql Server in early stage of their career. 
So, today I decided to post the solution for the same on my blog to help them.

Let's first see the problem encountered while using variable in "IN" clause.
First create a table with some sample data
create table tbldata(    
    id int, name varchar(50)
insert into tbldata
select 1, 'Sandeep' union all
select 2, 'Abhay' union all
select 3, 'Ritesh'

Now, let's execute the below queries with and without variable in "IN" clause.
select * from tbldata where name in ('Sandeep', 'Abhay')

Here, we get the result as expected, now let's execute query with variable
declare @idList varchar(max)
set @idList = '''Sandeep'', ''Abhay'''
select * from tbldata where name in (@idList) 

See here when we execute the same query with variable, we did not get the expected output. Hence, it is concluded that variable does not work fine when used inside "IN" clause in Sql Server.

Now, lets see the work around to this problem. There are two solutions to handle the same

1. Dynamic query
declare @query nvarchar(max), @idList varchar(max)
set @idList = '''Sandeep'', ''Abhay'''
set @query = 'select * from tbldata where name in (' + @idList + ')'
exec sp_executesql @query

2. Split function
First create split function using link: split function in sql
Once split function is created, then use the below solution
declare @idList varchar(max)
set @idList = 'Sandeep,Abhay'
select * from tbldata where name in (select val from dbo.split(@idList,','))

Replacing IN with JOIN
Split is a tabular function which returns table of the list passed to it, In the above example IN can be replaced with JOIN
declare @idList varchar(max)
set @idList = 'Sandeep,Abhay'
select  t.*
from    tbldata t
inner join dbo.split(@idList,',') s on t.name = s.val

If there are any other solution(s), everyone is welcome to share the same.
Share this article :

+ comments + 8 comments

March 25, 2013 at 1:28 PM

nice post :)

March 25, 2013 at 1:36 PM


April 16, 2013 at 9:49 AM

You are genious dude :))

April 16, 2013 at 10:21 AM

Thank a lot for the comment .... :)

August 2, 2013 at 8:48 PM

and for every DBMS that have not dynamic executions, or a bad performance, use:
select * from tbldata where instr ('|' || name || '|','|Sandeep||Abhay|')

The appended(delimitator) pipe character | can be replaced with a character of your choice.

November 6, 2013 at 2:15 AM

though not efficient but we can use charindex here to accomplish the same

November 16, 2013 at 11:12 PM

It shouldn't be done that way. If you have a dynamic list to check against, just put it in a table variable and join to that table. Your solutions (well, hacks, not solutions) have many rough edges...

November 17, 2013 at 12:30 PM

Dear Dog,
The idea here is to explain the problem while using variable in "IN" clause.
However, if you will go through the post carefully, in the last step it is done the same way you are suggesting and i would also suggest to use the same

Post a Comment