Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

SQL 'IN' Statement

Thread Tools
 
Search this Thread
 
Old 05 May 2007, 05:11 PM
  #1  
djuk
Scooby Regular
Thread Starter
 
djuk's Avatar
 
Join Date: Jun 2002
Location: Berkshire
Posts: 1,280
Likes: 0
Received 0 Likes on 0 Posts
Default SQL 'IN' Statement

I'm trying to figure out how to use the contents of a cell in Excel to populate a SQL IN statement but can't figure it out. Does anybody know if this is possible?

Using a standard IN statement works fine...

Code:
SELECT *
FROM CRMtest2.dbo.AgendaItems AgendaItems
WHERE (Region IN ('East','West'))
And using one based on a cell works fine if theres just a single value in the cell...

Code:
SELECT *
FROM CRMtest2.dbo.AgendaItems AgendaItems
WHERE (Region IN (?))


But when I enter multiple values like this...



The query fails

Any ideas welcome

D
Old 05 May 2007, 08:24 PM
  #2  
Dracoro
Scooby Regular
 
Dracoro's Avatar
 
Join Date: Sep 2001
Location: A powerslide near you
Posts: 10,261
Likes: 0
Received 0 Likes on 0 Posts
Default

you need to seperate the valus in the cells. as it stands, it's ONE string rather than 3. You need a way to seperate them out, putting in the single ' quote marks appropriately. I'll let you figure out how to do this but it's not that difficult. beware of single quotes within strings tho
Old 05 May 2007, 09:26 PM
  #3  
J4CKO
Scooby Regular
iTrader: (1)
 
J4CKO's Avatar
 
Join Date: Jan 2003
Posts: 19,384
Likes: 0
Received 1 Like on 1 Post
Default

Generate the whole select statement including the cell value as a text string and then run it, you will as Dracoro has said have to format the string with single quotes.
Old 05 May 2007, 10:54 PM
  #4  
djuk
Scooby Regular
Thread Starter
 
djuk's Avatar
 
Join Date: Jun 2002
Location: Berkshire
Posts: 1,280
Likes: 0
Received 0 Likes on 0 Posts
Default

Sorted, Thanks guys
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
stevem2k
Non Scooby Related
8
06 July 2011 11:11 PM
HHxx
Computer & Technology Related
3
16 January 2002 05:06 PM
Mkhan
Non Scooby Related
14
09 August 2001 02:07 AM
stevem2k
Non Scooby Related
3
05 July 2001 05:39 PM
SDB
Non Scooby Related
2
26 May 2001 11:55 PM



Quick Reply: SQL 'IN' Statement



All times are GMT +1. The time now is 06:11 PM.