Oracle Reports 中的日期计算

Date Calculation in Oracle Reports

我正在从 DATE_OUT (17-NOV-20 03:23 PM) 中减去 DATE_IN (16-NOV-20 06:02 PM)。我希望答案是 21.21 (hours.minutes),但它给了我 21.34,这是正确的时间,但不是正确的分钟。我做错了什么?

function TIME_CALCULATIONFormula return NUMBER is
begin
    RETURN ROUND((((:DATE_OUT - :DATE_IN)*24)*60)/60,2);
end;

您得到的结果是 而不是 “hours.minutes” - 它是小时数的十进制表示。 0.34 小时是 20 分 24 秒,这似乎是你想要的结果,直到你强制的两位小数的舍入误差。

该值是正确的,因为 0.34 小时 = 20.4 分钟。

如果你想return小时作为单位,分钟作为小数,那么你可以使用:

function TIME_CALCULATIONFormula return NUMBER
is
  diff INTERVAL DAY TO SECOND := (:DATE_OUT - :DATE_IN) DAY TO SECOND;
begin
  RETURN EXTRACT( DAY FROM diff ) * 24
         + EXTRACT( HOUR FROM diff )
         + EXTRACT( MINUTE FROM diff ) / 100;
end;

function TIME_CALCULATIONFormula return NUMBER
is
  diff INTERVAL DAY TO SECOND := CAST(:DATE_OUT AS TIMESTAMP)
                                 - CAST(:DATE_IN AS TIMESTAMP);
begin
  RETURN EXTRACT( DAY FROM diff ) * 24
         + EXTRACT( HOUR FROM diff )
         + EXTRACT( MINUTE FROM diff ) / 100;
end;

function TIME_CALCULATIONFormula return NUMBER
is
begin
  RETURN TRUNC( ( :date_out - :date_in ) * 24 ) +
         MOD( ( :date_out - :date_in ) * 24 * 60, 60 ) / 100;
end;

或者,如果您不希望时差超过 24 小时,并且希望在数字上添加尾随零:

function TIME_CALCULATIONFormula return VARCHAR2
is
begin
  RETURN TO_CHAR( DATE '1900-01-01' + ( :date_out - :date_in ), 'HH24.MI' );
end;

db<>fiddle here