Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 sql help

Author  Topic 

ann
Posting Yak Master

220 Posts

Posted - 2012-03-20 : 14:44:37
I always get this wrong. This is what my query looks like:

SELECT AcctID, Status, queuedTo
FROM Accts
where queuedto = 4 AND (Status != 'Canceled' OR status != 'sold'))

All I want are all the status's not equal to Canceled or Sold, but no matter how I write this, I always still end up with results with either

Can anyone help?

Thanks

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-20 : 14:52:36
[code]WHERE
queuedto = 4
AND STATUS NOT IN ( 'Canceled' ,'sold')[/code]
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-03-20 : 14:53:16
It doesn't sound like you want OR then, but I can't tell from your wording without a data sample. Try this:

SELECT AcctID, Status, queuedTo
FROM Accts
where queuedto = 4 AND Status <> 'Canceled' AND status <> 'sold'



Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2012-03-20 : 15:03:55
Neither of those results are right, because they ignore Null values.
Yes, I should have given example, here it is:

Table:
AcctID Status QueuedTo
1 Null 4
2 Active 4
3 Sold 5
4 Sold 4
5 Canceled 4

Results wanted:
1, Null, 4
2, Active, 4
Go to Top of Page

sunitabeck
Master Smack Fu Yak Hacker

5155 Posts

Posted - 2012-03-20 : 15:09:10
[code]WHERE
queuedto = 4
AND (STATUS NOT IN ( 'Canceled' ,'sold')
OR STATUS is NULL);
[/code]
Go to Top of Page

ann
Posting Yak Master

220 Posts

Posted - 2012-03-20 : 15:10:36
Thanks - that's it
Go to Top of Page
   

- Advertisement -