Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

MS Access SQL not returning what I expect

Posted on 2009-12-16
13
Medium Priority
?
243 Views
Last Modified: 2012-05-08
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

0
Comment
Question by:dyarosh
  • 7
  • 6
13 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26065986
please try this:

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

Author Comment

by:dyarosh
ID: 26066066
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.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26066117
SELECT     EmployeeID, EmployerID, LastName, FirstName, PS58 = nz(PS58, '')
FROM Employees
WHERE CDBL(PS58) > 0
ORDER BY LastName, FirstName;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:dyarosh
ID: 26066142
Still invalid use of null error
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26066172
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

0
 

Author Comment

by:dyarosh
ID: 26066202
Now I get Data type mismatch in criteria expression
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26066233
that means that you have some rows with PS58 not being numerical, right?
can you show data samples?
0
 

Author Comment

by:dyarosh
ID: 26066329
I'm including the database with all of the fields removed except for Employee ID, Employer ID and PS58 field.
Test.mdb.txt
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26066563
I cannot open that on my PC, as I don't have ms access installed @ home :(
0
 

Author Comment

by:dyarosh
ID: 26066680
Here is the employee table exported to a text file
Employees.txt
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 2000 total points
ID: 26066759
can you please try this:
SELECT     EmployeeID, EmployerID, LastName, FirstName, PS58 = nz(PS58, '')
FROM Employees
WHERE CDBL('0' & nz(PS58,'')) > 0
ORDER BY LastName, FirstName;

Open in new window

0
 

Author Closing Comment

by:dyarosh
ID: 31666972
Thank you.  That did the trick.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 26066909
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
0

Featured Post

[Webinar On Demand] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

580 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