Search code examples
oracle-databasefunctiondatecompiler-errorssubtraction

Oracle - PLS-00382: expression is of wrong type in subtracting dates


I have troubles compiling the following function in Oracle

CREATE OR REPLACE FUNCTION fn1
return binary_double
as
Fecha  DATE;
Dias binary_double;
begin
dbms_output.put_line(CAST(TRUNC(CURRENT_DATE,'DD') AS DATE) - cast(current_timestamp as date));
return Dias;
end;

It results in Error

Error(8,27): PLS-00382: expression is of wrong type

However running the same expression outside of function body

select CAST(TRUNC(CURRENT_DATE,'DD') AS DATE) - cast(current_timestamp as date) from dual

gives the expected result - number of days between two dates

-0.0607060185185185185185185185185185185185

Is there any ideas on what could be wrong about the function?


Solution

  • Use:

    begin
      dbms_output.put_line(TRUNC(CURRENT_DATE,'DD')  - cast( current_timestamp as date));
    end;
    /
    

    Current_date returns a value of datatype DATE
    Trunct also returns DATE.
    So You are trying to cast DATE type to DATE type.