The second parameter must be of the same datatype as the first parameter. The trunc
function is overloaded. Apparently when you pass it a null
, it interprets the value as a number and returns a number. Any of the following works:
SELECT NVL (NULL, TRUNC (SYSDATE)) FROM DUAL;
SELECT NVL (TRUNC (TO_DATE (NULL)), TRUNC (SYSDATE)) FROM DUAL;
SELECT NVL (TRUNC (CAST (NULL AS DATE)), TRUNC (SYSDATE)) FROM DUAL;
Follow up:
If the variable that is being used is properly defined as a date, then you should not have a problem. As an example, the following runs without error:
DECLARE
FUNCTION f
RETURN DATE IS
BEGIN
RETURN NULL;
END f;
BEGIN
DBMS_OUTPUT.put_line (NVL (TRUNC (f), TRUNC (SYSDATE)));
END;
In order to get an answer that actually solves your problem, I would recommend editing the question to include code that actually demonstrates the problem.