?
Solved

Access Query - Null in where statement issue

Posted on 2014-07-30
4
Medium Priority
?
361 Views
Last Modified: 2014-08-01
Hi,

When I run the following query the results does not return rows where CHARGE is null. Instead it returns rows where CHARGE is anything but null.

 SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (((HLabor.CHARGE) Is Null) AND ((HLabor.DECLINED)=False) AND ((HLabor.CATAGORY)="00") AND ((HLabor.LABOR_OP)="001")) OR (((HLabor.CATAGORY)="") AND ((HLabor.LABOR_OP)=""));

Open in new window


I run the same query without the additional criteria and it returns just the rows where CHARGE is = null:

SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (((HLabor.CHARGE) Is Null));

Open in new window


Why is the first query ignoring the request for rows where CHARGE is null in addition to the other criteria? Is there a workaround?
0
Comment
Question by:ckelsoe
[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
  • 2
4 Comments
 
LVL 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 40230718
Have a look at your usage of brackets (parenthesis):
SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (((HLabor.CHARGE) Is Null) AND ((HLabor.DECLINED)=False) AND ((HLabor.CATAGORY)="00") AND ((HLabor.LABOR_OP)="001")) OR (((HLabor.CATAGORY)="") AND ((HLabor.LABOR_OP)=""));

Open in new window

If we take away the extra brackets Access has inserted, the query can be simplified to this:
SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (
        (HLabor.CHARGE Is Null) AND (HLabor.DECLINED=False) AND (HLabor.CATAGORY="00") AND (HLabor.LABOR_OP="001")
      ) 
      OR 
      (
        (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")
      );

Open in new window

have a look what I have done. I have made the where clause more readable by separating two big criteria. Now, as you can see the criteria where you want the Charge IS NULL also includes these:
(HLabor.CHARGE Is Null) AND (HLabor.DECLINED=False) AND (HLabor.CATAGORY="00") AND (HLabor.LABOR_OP="001")

Open in new window

However, the second criteria is also asking to select additional rows for
 (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")

Open in new window

See, how these additional rows do not request the Charge Is NULL. If you want these additional rows to also satisfy the condition for  Charge Is NULL you either include it here:
(HLabor.CHARGE Is Null) AND (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")

Open in new window

or, even better, reformat your where clause like this:
SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE (HLabor.CHARGE Is Null) AND 
      (
         (
            (HLabor.DECLINED=False) AND (HLabor.CATAGORY="00") AND (HLabor.LABOR_OP="001")
         ) 
         OR 
         (
            (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")
         )
      );

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 40230956
This applies the Is Null condition on both sides of the OR operator:
SELECT Val(HLabor.RO_NO) AS RO_NO, HLabor.DESC_LINES, HLabor.CHARGE, HLabor.DECLINED, 
HLabor.CATAGORY, HLabor.LABOR_OP
FROM HLabor
WHERE 
(((HLabor.CHARGE) Is Null) 
AND ((HLabor.DECLINED)=False) 
AND ((HLabor.CATAGORY)="00") 
AND ((HLabor.LABOR_OP)="001")) 

OR 
(((HLabor.CHARGE) Is Null) AND ((HLabor.CATAGORY)="") AND ((HLabor.LABOR_OP)=""));

Open in new window

0
 
LVL 25

Expert Comment

by:chaau
ID: 40230961
Yes, it does. This is what I proposed as a first option:
If you want these additional rows to also satisfy the condition for  Charge Is NULL you either include it here:
(HLabor.CHARGE Is Null) AND (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")
However, you can make it more readable if you use the second option I have proposed:
WHERE (HLabor.CHARGE Is Null) AND 
      (
         (
            (HLabor.DECLINED=False) AND (HLabor.CATAGORY="00") AND (HLabor.LABOR_OP="001")
         ) 
         OR 
         (
            (HLabor.CATAGORY="") AND (HLabor.LABOR_OP="")
         )
      );

Open in new window

0
 

Author Closing Comment

by:ckelsoe
ID: 40235571
Thanks - that worked perfect.
0

Featured Post

Get proactive database performance tuning online

At Percona’s web store you can order full Percona Database Performance Audit in minutes. Find out the health of your database, and how to improve it. Pay online with a credit card. Improve your database performance now!

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Suggested Courses

764 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