Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Not equal criteria in query

Status
Not open for further replies.

mmogul

IS-IT--Management
Dec 1, 2003
218
US
I am running a simple query that excludes a value in a particular column. This is a text column that can contain null values.

For example, the table might have two columns: a1 and a2 and have data like:

a1 a2
a x
a x
a x
b x
b x
b x
c x
c x
c x
s
sd
ss

When I run a query and set the criteria for field a1 as:
Not "c"

The query excludes records that are not equal to "c" but also excludes all records that are equal to null. I would expect the records where field a1 is null to be displayed.

Can someone explain why this occurs?

Thanks.


Thanks.
 

It would be easier to evaluate possible errors if you were to show your query SQL.

Randy
 
Consider NULL as unknown and then you'll understand that NULL is NOT 'C'
You may try this (SQL code)
SELECT *
FROM yourTable
WHERE Nz(a1,"?")<>"C"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I would expect the records where field a1 is null to be displayed.

That is incorrect. If you want to include them use the solution provided by JoeAtWork.

Greg
People demand freedom of speech as a compensation for the freedom of thought which they seldom use. Kierkegaard
 
dhookom said:
it looks at field a2 rather than a1
Yes I picked the wrong field but it is the syntax that is important.
Reasons I prefer my solution:
1. I expect "pure SQL" to be more efficient than the NZ function
2. I don't trust that concatenating a Null to an empty string will always be supported in the future
3. My syntax works also for SQL Server and Oracle - useful if the OP ever upgrades to one of those platforms
4. I find it the easiest to read, it is the most explicit as to what is actually being checked for
 
Good points JoeAtWork. If there are other criteria in the where clause you only have to remember to put ()s around the ORs:
Code:
WHERE (a1 <> "C" OR a1 IS NULL) AND ...

Duane
Hook'D on Access
MS Access MVP
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top