SQL AND

Hi Experts,

I have the following query
SELECT tblPayers.fldPayerID, tblPayers.fldFName, tblPayers.fldLName, [fldArchived]*(-1) AS archived
FROM tblPayers
WHERE (((tblPayers.fldFName)<>"") AND ((tblPayers.fldLName)<>""))
ORDER BY tblPayers.fldPayerID;

Open in new window


My code is to exclude all records where both fName and lName are blank, but do include if either one contains something, but this query returns:

fldPayerID      fldFName      fldLName      archived
3      Tim      Cooper      0
6      A      N      1

Where, without my WHERE, I get

fldPayerID      fldFName      fldLName      archived
1      DEPOSIT            0
2      N/A            0
3      Tim      Cooper      0
4      paycheque            0
5      Envelope to Tim            0
6      A      N      1

In this case, I expect to get all 6 records with and without the WHERE.

What am I doing wrong?
APD TorontoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Scott McDaniel (Microsoft Access MVP - EE MVE )Infotrakker SoftwareCommented:
Perhaps this:

SELECT tblPayers.fldPayerID, tblPayers.fldFName, tblPayers.fldLName, [fldArchived]*(-1) AS archived
FROM tblPayers
WHERE (((tblPayers.fldFName)<>"" AND (tblPayers.fldLName)<>""))
ORDER BY tblPayers.fldPayerID;
0
Aneesh RetnakaranDatabase AdministratorCommented:
It could be NULL

SELECT tblPayers.fldPayerID, tblPayers.fldFName, tblPayers.fldLName, [fldArchived]*(-1) AS archived
FROM tblPayers
WHERE ISNULL(tblPayers.fldFName,'')<>'' AND ISNULL(tblPayers.fldLName,'')<>''
ORDER BY tblPayers.fldPayerID;
0
APD TorontoSoftware DeveloperAuthor Commented:
Scott, same results.

Aneesh, syntax error.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Rey Obrero (Capricorn1)Commented:
try

 SELECT tblPayers.fldPayerID, tblPayers.fldFName, tblPayers.fldLName, [fldArchived]*(-1) AS archived
FROM tblPayers
WHERE [tblPayers].[fldFName] & [tblPayers].[fldLName] & ""<>""
ORDER BY tblPayers.fldPayerID;
0
APD TorontoSoftware DeveloperAuthor Commented:
This works, but why & ""<>"" and not <> ""  ?
0
Scott PletcherSenior DBACommented:
WHERE NOT (tblPayers.fldFName = "" AND tblPayers.fldLName = "")
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rey Obrero (Capricorn1)Commented:
that is to force the null values to a zero length string
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.