Solved

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

Posted on 2015-02-06
5
76 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
  • 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 68

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 68

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.

Join & Write a Comment

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now