Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using the IN statement

Status
Not open for further replies.

vlitim

Programmer
Joined
Sep 2, 2000
Messages
393
Location
GB
I have a comma delimited list in a column and I want to see if a number appears in it ie.

select * from table where 68 IN (staffID)

now it works if there is only one number in the col but as soon as more appear nothing is returned

I am sure I have this working somewhere before but now it seems beyond me! Obviously I am doing something wrong

Cheers
Tim
 
Hi Tim,

check out FAQ183-3132
 
I am doing it the other way around though so

@staffID = 3

select * from table where @staffID IN (staffID)

where staffID in the table = '3,35,65'

Tim
 
Code:
select * from t where 
charindex(',68,',',' + staffID + ',') > 0
 
Doh!
sorry Tim, i've obviously got a touch of the fridays
 
swampBoogie I have tried that but if there is nothing in the staffID col then it doesn't return the record which it should do.
 
So if the staffid column is NULL then you always want the row returned, but if there are values in staffid then you only want it returned if it contains the value you pass in?

Code:
SELECT * FROM t
WHERE CHARINDEX(',' + CAST(@staffid AS varchar) + ',', ',' + staffid + ',') > 0
  OR staffid IS NULL

--James
 

Try this:

select * from table where replace(staffID, ' ', '') like '%,' + @staffID + ',%' OR staffid IS NULL

This is more robust cause it handles '3, 35 , 65'
 
Why can't you use the IN statement for this??
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top