We help IT Professionals succeed at work.

How to use WHERE IN with a list of values from a table

250 Views
Last Modified: 2010-04-21
Hi
I have a table which contains a list of values, for example :

id      cats
1       2000,2001,2002,2004,2005
2       20010,20011,20014,20015  

What I want is to be able to put the "cats" field into a WHERE IN statement within a stored procedure that I have written in SQL Server 2005.  Unsurprisingly really I get "Conversion failed when converting the varchar value to int" error because WHERE IN takes an int but I need to have a comma delimited list of values ("cats" in the example) to match against.

Any help would be gratefully received!
Comment
Watch Question

CERTIFIED EXPERT
Awarded 2008
Awarded 2008

Commented:
you'll have to do it dynamically...post the code you're currently using, and I'll see if I can doctor it up for you.
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
use cast\convert function to convert into varchar
more:
 http://msdn.microsoft.com/en-us/library/ms187928.aspx

Author

Commented:
Hi
The code is a basic SELECT statement from one table but where cats in (2,3,4,5,100) or whichever set of values I need to use depending on the query. When I do a list like "..WHERE IN (2,3,4)" it works but when I try
SELECT * FROM tblX WHERE IN (SELECT cats FROM tblY WHERE ID=1) it doesn't.  

Is it possible to convert/cast a list of comma separated ints into one int value?
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
you need to put column name : where column in (select...)
sELECT * FROM tblX WHERE  .... IN (SELECT cats FROM tblY WHERE ID=1
                                                         /\
probably:
sELECT * FROM tblX WHERE  cats IN (SELECT cats FROM tblY WHERE ID=1

Author

Commented:
Hi
Sorry I meant to put the column name before the IN. Syntactically I have got it ok, it's just the conversion of the IN ( ) statement that I'm having an issue with.
CERTIFIED EXPERT
Awarded 2008
Awarded 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks I think I may be able to sort something out with this function.
EugeneZSQL SERVER EXPERT
CERTIFIED EXPERT

Commented:
hmm:
if you get right result from

sELECT * FROM tblX WHERE  cats IN (1,2,3)
and
SELECT cats FROM tblY WHERE ID=1  
gives you
1
2
3
---the query below will give you desired result unless instead cats you need to use catID column:

sELECT * FROM tblX WHERE  cats IN (SELECT cats FROM tblY WHERE ID=1 )

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.