T-SQL: Select WHERE IN varchar

Hi,
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

LVL 4
karakavAsked:
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 DulicCommented:
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

0
puranik_pCommented:
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

0
Guy Hengel [angelIII / a3]Billing EngineerCommented:
please create the function dbo.ParmsToList as shown in the last comment here:
http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/Q_21627393.html

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

0

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.
0
karakavAuthor Commented:
Thanks a lot.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.