Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more: , +
The following query checks whether value exist in particular columns or not.

SQL
Select * from ACCOUNT where 'UGEREA0000OE' in (MODIFYUSER,CREATEUSER)


however this is only working for single value....what can i use for multiple values i.e.

SQL
Select * from ACCOUNT where 'UGEREA0000OE','UGEREA0000OF' in (MODIFYUSER,CREATEUSER)


This is not working. Please let me knw how can i do this.
Thanks.
Posted
Updated 14-Jan-15 7:00am
v2
Comments
ZurdoDev 14-Jan-15 12:44pm    
Just do WHERE 1 IN (a,b) OR 2 IN (a,b)
lovejeet0707 14-Jan-15 12:47pm    
You mean this :
Select * from ACCOUNT where 1 in ('UGEREA0000OB','UGEREA0000OE') or 2 in ('UGEREA0000OB','UGEREA0000OE') in (MODIFYUSER,CREATEUSER) ??

However this is not working.
PIEBALDconsult 14-Jan-15 13:06pm    
Something like Ryan suggest is probably the simplest, but you could also use a UNION or JOIN to a CTE if you like and your data allows.
lovejeet0707 14-Jan-15 13:08pm    
Yeah its looking simple, but didnt work.
Can you re-write the query how it will be done
PIEBALDconsult 14-Jan-15 13:11pm    
Can. Won't.

1 solution

You can't supply multiple values to an SQL IN - you can only specify a single column on the left [of the IN keyword], and none on the right.
Try:
SQL
SELECT * FROM Acount WHERE MuodifyUser IN ('UGEREA0000OE','UGEREA0000OF') OR CreateUser IN ('UGEREA0000OE','UGEREA0000OF')


[edit]Typos, and a clarifcation - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
PIEBALDconsult 14-Jan-15 13:02pm    
"you can only specify a single column of the left, and none "

Umm... what?
lovejeet0707 14-Jan-15 13:03pm    
Sorry didnt get uhh ??
OriginalGriff 14-Jan-15 13:58pm    
Typos...:sigh:
PIEBALDconsult 14-Jan-15 14:05pm    
I allowed for that, but your statement still seems untrue (of SQL Server).
OriginalGriff 14-Jan-15 14:16pm    
In what way?
I'm pretty sure the IN syntax is
exp IN (list)
And exp can only be a single value (one column for example), and list can't contain columns - it has to be constant values.
So to check if either of two columns is in a range of values, you need two INs and and OR - don't you?

Both W3Schools and MSDN seem to agree, pretty much (MSDN adds a subquery as permissible as the IN list, is all):
http://www.w3schools.com/sql/sql_in.asp
http://msdn.microsoft.com/en-us/library/ms177682.aspx

If I'm wrong, I'd be very happy about it (given IN is a PITA when you try to get "creative")! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900