If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > need help using 'IN'

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-09-04, 10:48
andyhuck andyhuck is offline
Registered User
 
Join Date: Sep 2004
Posts: 5
Question need help using 'IN'

Here is my problem.

I want to have a field in a database the will hold a string that resembles parameters for the IN. For example, the cell would contain

" 'a','b','c' "

I want to store this information into a variable and then in my sql statement, using it as the parameter for the IN function. Here's a look at the whole picture.

@variable = SELECT field1 FROM temp
(@variable now contains 'a','b','c')

SELECT * FROM temp2 WHERE letters IN (@variable)

This works if there is only one item in the field and there are no single quotes around it. I haven't been able to figure out how this can be done using single quotes around a single item or multiple items like in my example.

Thanks in advance
Andy
Reply With Quote
  #2 (permalink)  
Old 11-09-04, 12:40
andyhuck andyhuck is offline
Registered User
 
Join Date: Sep 2004
Posts: 5
I've found this post

http://www.dbforums.com/t988076.html

but this is not what i need because i'm planning on using the results from the query in a WHERE clause of another query.

i.e.

SELECT * FROM table WHERE column IN (SELECT column FROM temp WHERE letters IN (@variable))
Reply With Quote
  #3 (permalink)  
Old 11-09-04, 13:55
urquel urquel is offline
Registered User
 
Join Date: Aug 2004
Posts: 330
Have you considered normalizing your design so that the values 'a', 'b', 'c' are stored in separate rows?
Reply With Quote
  #4 (permalink)  
Old 11-09-04, 15:42
LKBrwn_DBA LKBrwn_DBA is offline
Registered User
 
Join Date: Jun 2003
Location: West Palm Beach, FL
Posts: 2,455
Cool

Try something like this:
Code:
DECLARE @variable TABLE (f1 VARCHAR(1))
INSERT INTO @variable SELECT field1 FROM temp
SELECT * FROM temp2 WHERE letters IN (SELECT * FROM @variable)

__________________
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On