Solved

Access Query - Null in where statement issue

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

Suggested Solutions

Title # Comments Views Activity
ms access vba split-screen form needs to be refreshed on load 7 31
string fuctions 4 26
Email Header Detail 12 55
SQL Error - Query 6 26
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…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
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 Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

772 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