简体   繁体   English

Function 计算 2 个日期之间的天数,接受开始日期和结束日期,然后返回在该期间覆盖的天数

[英]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

I have to do something similar to this, and I need to spool file as well to css我必须做类似的事情,我还需要将文件假脱机到 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;
/

Your sample code is almost there.您的示例代码几乎就在那里。 You just need to add a second in parameter.您只需要in参数中添加第二个。 Also, you need to return the result, so it gets back to the caller.此外,您需要return结果,以便返回给调用者。 Finally, there is no need for an intermediate variable.最后,不需要中间变量。 You can compute and return immediatly.您可以立即计算并返回。

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;
/

Then you can do:然后你可以这样做:

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

Yields:产量:

| RES |
| --: |
|   5 |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM