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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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;
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;
APD TorontoSoftware DeveloperAuthor Commented:
Scott, same results.

Aneesh, syntax error.
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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;
APD TorontoSoftware DeveloperAuthor Commented:
This works, but why & ""<>"" and not <> ""  ?
Scott PletcherSenior DBACommented:
WHERE NOT (tblPayers.fldFName = "" AND tblPayers.fldLName = "")

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
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.