Passing Parameter Value = NULL to MS Query in Excel 2013

I have an Excel 2013 table in a sheet which is based on an MS Query into a SQL database.  There are three parameters in the query, and values for those parameters are pulled from reference cells in another sheet.  Here's how the parameters look in the query's SQL code:

    WHERE ((Fac=?)
    AND (Date>=? AND Date<=?)
    AND (SA=?))

... and here are the parameter reference cells:

            A            B
    =================
    1 Facility   CC
    2 Start           03/30/15
    3 Stop           04/04/15
    4 SA           Y

The Fac and the Date range parameters work just fine.  But the SA parameter is giving me fits because the possible values in the SA field (defined as VARCHAR(1)) are either "Y" or NULL (blank).  That is, I can put "Y" in Cell B4 above and all works well to return all records where SA = "Y".

But I can't figure out what to put in B4 to return only the records where SA = NULL: Making Cell B4 blank "works" but returns no records, and putting anything more than one character long in Cell B4 (e.g., <>'Y') returns a "String data, right truncation" error (presumably because it's VARCHAR(1)?).  And, at times I would want all records (e.g., both SA = "Y" and SA = NULL).  

This works, though, while keeping Cell B4 blank:

    (SA=? OR SA IS NULL)

... to return only SA = NULL records.  But with this I have no way to get just the SA = "Y" records, because putting "Y" in Cell B4 returns both SA = "Y and SA = NULL.

Am I missing something obvious?

Thanks,
Curt
LVL 1
curtwadeAsked:
Who is Participating?
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
in SQL, to check for NULL, you need to do this:

WHERE yourfield IS NULL

and not where yourfield = NULL (though with some ANSI NULL settings in sql server, you could do it)

which means you need to change the sql like this:
WHERE (   ( (Fac=? )  or (? = '' AND Fac IS NULL) )  ....

and pass the field value of Fac 2 times;
0
 
curtwadeAuthor Commented:
Thanks for reply, Guy!  I've been offsite the past few days and will play with your suggestion as soon as I can.
0
 
Martin LissOlder than dirtCommented:
I've requested that this question be closed as follows:

Accepted answer: 500 points for Guy Hengel [angelIII / a3]'s comment #a40737361

for the following reason:

This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
 
curtwadeAuthor Commented:
It actually did not give me the results I wanted, but set me on the right path.  Thanks!  And sorry for the delayed response - I was put onto other projects for awhile.
0
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.

All Courses

From novice to tech pro — start learning today.