Solved

Access Query - Null in where statement issue

Posted on 2014-07-30
4
356 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
  • 2
4 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 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 45

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 24

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

The first two articles in this short series — Using a Criteria Form to Filter Records (http://www.experts-exchange.com/A_6069.html) and Building a Custom Filter (http://www.experts-exchange.com/A_6070.html) — discuss in some detail how a form can be…
PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

911 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

16 Experts available now in Live!

Get 1:1 Help Now