Link to home
Start Free TrialLog in
Avatar of dyarosh
dyarosh

asked on

MS Access SQL not returning what I expect

I have an MS Access database with an Employees Table.  The Employees table has a field, PS58, that is a text field that contains a number or null.  I'm trying to format a query that will display all the records whose PS58 value is greater than 0.  I haven't been able to figure out the correct syntax for the query.  The SQL 1 select statement below gives me an Invalid Use of Null error.  
SQL 1
SELECT     EmployeeID, EmployerID, LastName, FirstName, PS58
FROM Employees
ORDER BY LastName, FirstName
WHERE CDBL(PS58) > 0;

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

please try this:

SELECT     EmployeeID, EmployerID, LastName, FirstName, nz(PS58, '') PS58
FROM Employees
WHERE CDBL(PS58) > 0
ORDER BY LastName, FirstName
;
Avatar of dyarosh
dyarosh

ASKER

I got a syntax error when I tried your query so I changed it to the following:
SELECT     EmployeeID, EmployerID, LastName, FirstName, nz(PS58, ''), PS58
FROM Employees
WHERE CDBL(PS58) > 0
ORDER BY LastName, FirstName;

This query still gives me an Invalid use of Null error.
SELECT     EmployeeID, EmployerID, LastName, FirstName, PS58 = nz(PS58, '')
FROM Employees
WHERE CDBL(PS58) > 0
ORDER BY LastName, FirstName;
Avatar of dyarosh

ASKER

Still invalid use of null error
sorry, I overlooked the WHERE clause part:
SELECT     EmployeeID, EmployerID, LastName, FirstName, PS58 = nz(PS58, '')
FROM Employees
WHERE CDBL(nz(PS58,'')) > 0
ORDER BY LastName, FirstName;

Open in new window

Avatar of dyarosh

ASKER

Now I get Data type mismatch in criteria expression
that means that you have some rows with PS58 not being numerical, right?
can you show data samples?
Avatar of dyarosh

ASKER

I'm including the database with all of the fields removed except for Employee ID, Employer ID and PS58 field.
Test.mdb.txt
I cannot open that on my PC, as I don't have ms access installed @ home :(
Avatar of dyarosh

ASKER

Here is the employee table exported to a text file
Employees.txt
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dyarosh

ASKER

Thank you.  That did the trick.
for anyone reading:
§ cdbl( null )   => raises in the invalid use of NULL
§ cdbl( '' )     => raises the Data type mismatch in criteria expression

so,
§ the nz() function ensures we don't get a NULL
§ the '0' & ...  ensures we don't get a empty string, but '0' at least, and for any really numerical value, the leading '0' won't change anything

glad I could help