Solved

Access Query - Null in where statement issue

Posted on 2014-07-30
4
360 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 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 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

Stressed Out?

Watch some penguins on the livecam!

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
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…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

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