Solved

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

Posted on 2015-02-06
5
78 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 69

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 69

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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Some sers suddenly getting error popup msg 28 84
Oracle DB monitor SW 21 47
Tsql query 6 18
SQL Syntax: How to force case sensitive query? 2 20
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

813 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

10 Experts available now in Live!

Get 1:1 Help Now