The statement is
SELECT * FROM tableA WHERE x = ?
and the parameter is inserted via java.sql.PreparedStatement 'stmt'
stmt.setString(1, y); // y may be null
If y
is null, the statement returns no rows in every case because x = null
is always false (should be x IS NULL
).
One solution would be
SELECT * FROM tableA WHERE x = ? OR (x IS NULL AND ? IS NULL)
But then i have to set the same parameter twice. Is there a better solution?
Thanks!
There is a quite unknown ANSI-SQL operator
IS DISTINCT FROM
that handles NULL values. It can be used like that:So only one parameter has to be set. Unfortunately, this is not supported by MS SQL Server (2008).
Another solution could be, if there is a value that is and will be never used ('XXX'):
In Oracle 11g, I do it this way because
x = null
technically evaluates toUNKNOWN
:The expression before the
OR
takes care of equating NULL with NULL, then the expression after takes care of all other possibilities.LNNVL
changesUNKNOWN
toTRUE
,TRUE
toFALSE
andFALSE
toTRUE
, which is the exact opposite of what we want, hence theNOT
.The accepted solution didn't work for me in Oracle in some cases, when it was part of a larger expression, involving a
NOT
.I've always done it the way you show in your question. Setting the same parameter twice is not such a huge hardship, is it?
would just use 2 different statements:
Statement 1:
Statement 2:
You can check your variable and build the proper statement depending on the condition. I think this makes the code much clearer and easier to understand.
EDIT By the way, why not use stored procedures? Then you can handle all this NULL logic in the SP and you can simplify things on the front end call.
If you use for instance mysql you could probably do something like:
Then yo could do:
You would have to check your explain plan to see if it improves your performance. It though would mean that the empty string is equal to null, so it is not granted it would fit your needs.