?
Solved

Why is "not equal to" not working in SQL Server

Posted on 2015-02-06
5
Medium Priority
?
82 Views
Last Modified: 2015-02-06
SELECT     TOP (100) PERCENT dbo.Job.Job, dbo.Job.Customer, dbo.Job.Rev, dbo.Job.Part_Number, dbo.Job.Description, dbo.Job.Status, dbo.Delivery.Promised_Quantity, 
                      dbo.Job.Order_Unit, dbo.Delivery.Promised_Date, dbo.Delivery.Comment, dbo.Job_Operation.Work_Center, dbo.Job_Operation.Status AS Expr1, 
                      dbo.Delivery.Remaining_Quantity, dbo.Job_Operation.Note_Text, ISNULL(SUBSTRING(dbo.Job_Operation.Note_Text, 11, 15), 'No Date') AS Expr2, 
                      dbo.Job_Operation.Est_Total_Hrs
FROM         dbo.Job INNER JOIN
                      dbo.Job_Operation ON dbo.Job.Job = dbo.Job_Operation.Job INNER JOIN
                      dbo.Delivery ON dbo.Job.Job = dbo.Delivery.Job
WHERE     (dbo.Job.Status = 'active') AND (dbo.Job_Operation.Status = 'o' OR
                      dbo.Job_Operation.Status = 's') AND (dbo.Delivery.Remaining_Quantity > 0) AND (dbo.Job_Operation.Work_Center <> 'DELV-FLATB' OR
                      dbo.Job_Operation.Work_Center <> 'DELV-COMBO' OR
                      dbo.Job_Operation.Work_Center <> 'DELV-PICKU')

Open in new window


Please see attached image.
Capture.JPG
0
Comment
Question by:princeservice
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40593589
You should have "AND", not "OR", i.e.

(dbo.Job_Operation.Work_Center <> 'DELV-FLATB' AND
                      dbo.Job_Operation.Work_Center <> 'DELV-COMBO' AND
                      dbo.Job_Operation.Work_Center <> 'DELV-PICKU')
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40593594
The reason is that the OR evaluates each condition separately, and if ONE (or more) of them is true, says True.

The AND requires that ALL of them are true.

So, if dbo.Job_Operation.Work_Center = 'DELV-FLATB' , then

dbo.Job_Operation.Work_Center <> 'DELV-FLATB' - that's false
dbo.Job_Operation.Work_Center <> 'DELV-COMBO' - that's true
dbo.Job_Operation.Work_Center <> 'DELV-PICKU' - that's true.

Because more than zero were true, OR returns True.
However, because they weren't all true, AND returns False.
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 40593596
or use IN instead:
dbo.Job_Operation.Work_Center NOT IN ('DELV-FLATB', 'DELV-COMBO', 'DELV-PICKU')

Open in new window

0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40593606
However, because they weren't all true, AND returns False.
That is not correct in regard of the OR subexpression. The ORs are always true, as you correctly stated. The AND is redundant then, because
   x AND TRUE
is always   x.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40593646
Which is what I said.
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question