N nrsgzz Member Feb 16, 2014 #1 Hi all. I have a query that uses variables, something like this SQL: SELECT * FROM MYTAB A WHERE A.COL1 = :COL1 AND A.COL2 = :COL2 However the variables could be null and in that case the query will never return any results. How can I make it work?
Hi all. I have a query that uses variables, something like this SQL: SELECT * FROM MYTAB A WHERE A.COL1 = :COL1 AND A.COL2 = :COL2 However the variables could be null and in that case the query will never return any results. How can I make it work?
Marco Kalter Administrator Staff member Feb 17, 2014 #1 Comparing with NULL will resolve to NULL, which is neither True nor False. You can change the query as follows: Code: SELECT * FROM MYTAB A WHERE (A.COL1 = :COL1 OR (A.COL1 IS NULL AND :COL1 IS NULL)) AND (A.COL2 = :COL2 OR (A.COL2 IS NULL AND :COL2 IS NULL))
Comparing with NULL will resolve to NULL, which is neither True nor False. You can change the query as follows: Code: SELECT * FROM MYTAB A WHERE (A.COL1 = :COL1 OR (A.COL1 IS NULL AND :COL1 IS NULL)) AND (A.COL2 = :COL2 OR (A.COL2 IS NULL AND :COL2 IS NULL))