计算两个日期之间的天数的函数,接受开始日期和结束日期,然后 returns 它在该期间涵盖了多少天

Function that calculates number of days between 2 dates, accepts the start date and end date, then returns how many days it covers over that period

我必须做类似的事情,我还需要假脱机文件 css

CREATE OR REPLACE FUNCTION NumberOfDays
(in_date IN DATE)
RETURN NUMBER
IS
days NUMBER;
BEGIN
days:= TRUNC(SYSDATE - in_date);
EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
/

您的示例代码就快完成了。您只需要添加第二个 in 参数。此外,您需要 return 结果,以便返回给调用者。最后,不需要中间变量。您可以立即计算并return。

create or replace function number_of_days(start_date in date, end_date in date)
return number
is
begin
    return trunc(end_date - start_date);
exception
    when others then
    dbms_output.put_line(sqlerrm);
end;
/

那么你可以这样做:

select number_of_days(date '2020-06-01', date '2020-06-06') res from dual

产量:

| RES |
| --: |
|   5 |