Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2004
    Posts
    5

    Question Unanswered: 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

  2. #2
    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))

  3. #3
    Join Date
    Aug 2004
    Posts
    330
    Have you considered normalizing your design so that the values 'a', 'b', 'c' are stored in separate rows?

  4. #4
    Join Date
    Jun 2003
    Location
    West Palm Beach, FL
    Posts
    2,713

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •