SQL "OR" Clause question

Is there a more simple way to do this instead of using the or clause?
SELECT  * from mtytable where (accountNo='1' or accountNo='345' or accountNo='2' or accountNo='4' or accountNo='4000' or ...)

I'm looking for something like
SELECT  * from mtytable where accountNo in ('1', '345', '2','4',...... )


Thanks!
chrisliAsked:
Who is Participating?
 
MikeOM_DBAConnect With a Mentor Commented:
Yes, exactly as you coded using the "IN ()" construct.
:p
0
 
sdstuberConnect With a Mentor Commented:
if your account numbers are in fact NUMBERS then you should remove the quotes, otherwise your syntax is correct as is


that would apply to both the OR and the IN versions
0
 
chrisliAuthor Commented:
I'm feeling pretty stupid now... But how come this is not returning any results?

DECLARE @accounts VARCHAR(max)
Select @accounts = COALESCE( '''' + LTRIM(RTRIM(AccountNo))+ ''', ' + @accounts,' ''' + LTRIM(RTRIM(AccountNo)) + '''') from mytable where [type]='newAccts'

SELECT     * FROM theothertable WHERE    (accountNo IN (@accounts))
0
 
sdstuberConnect With a Mentor Commented:
because you are using a variable.  a variable (a single value) and a list of values aren't the same thing.
0
 
sdstuberConnect With a Mentor Commented:
don't bother trying to construct the list,  simply use the query itself as the IN condition


select * from theothertable where accountno in
(select accountno from mytable where [type]='newAccts')
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.