Solved

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

Posted on 2015-02-06
5
81 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 500 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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

695 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