T-SQL: Select WHERE IN varchar

Can some one tell me how to use the IN keyword to select rows from a table whose one column's value is already defined in an array? Here is my situation: provided a 'array' let say 'value1,value2,value3' I would like to do the following:
declare @values varchar(max)
set @values = 'value1,value2,value3' 
select * from table1 where colum1 in @values

Open in new window

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Haris DulicIT ArchitectCommented:
i had similar issue and this is how i solved it ...

CREATE TABLE  #upit( value varchar(2) )
insert into #upit  values ( '01' )
insert into #upit  values ( '02' )
select * from table1 where colum1 in (select value from #temp)

Open in new window

How about this....
declare @values varchar(max)
declare @select nvarchar(1024)
set @values = 'value1,value2,value3' 
SET @select = 'select * from table1 where colum1 in (' + @values + ')'
EXEC sp_executesql @select

Open in new window

Guy Hengel [angelIII / a3]Billing EngineerCommented:
please create the function dbo.ParmsToList as shown in the last comment here:

and then, use it like this:

declare @values varchar(max)
set @values = 'value1,value2,value3' 
select * from table1 where colum1 in (select value from dbo.ParmsToList(@values,','))

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
karakavAuthor Commented:
puranik_p, your solution is not working as values in @values are considered columns.
karakavAuthor Commented:
Thanks a lot.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.