1

我想问我如何检查两个日期之间的差异。

  1. BillingDate 是一个日期类型,带有条目 'DD-MON-YYYY'

  2. 另一个日期是当前日期。

sys_date - BillingDate = daysFromBilled

我发现很多例子他们实际上说明了第二个日期来计算差异,但我正在寻找的是当前日期之间的差异,所以我可以将它添加到日程安排或工作中。

我正在使用甲骨文顺便说一句。


还有一点要补充,我会继续搜索,但如果你也可以推荐,我应该如何实现这样的功能:

  1. 计算所有 BillingDate 条目的日期差异
  2. 如果差异超过 30 天,则触发更改表,将状态设置为延迟。
  3. 如果状态超过 60 天,服务属性将被更改并更改为切割

这是我粗略的表格布局

Cust             Billing
--------         ----------
CustID(PK)       BillingID(PK)
LateStatus       LateStatus
Service          BillingDate
                 CustID

非常感谢。

更新

REPLACE view DateDifference as select trunc(sysdate)- trunc(BillingDate) from Billing;

seems legit.

4

2 回答 2

1

Simply subtract one date from the other:

BillingDate - sysdate

To do that in a select statement, just use it like this:

select billingdate - sysdate as daysFromBilled
from ...

Inside a trigger you use a regular assignment operator:

declare
    daysFromBilled integer;

begin 
    daysFromBilled := :new.billingdate - sysdate;
...

that will return the number of days, including fractional values if the time is different (a DATE column in Oracle also contains a time!).

If you only want to get full days, use this:

trunc(BillingDate) - trunc(sysdate)

This statement of yours:

date type with an entry 'DD-MON-YYYY'

Indicates a misunderstanding on how DATE values work.

A DATE (or TIMESTAMP) does not have any format.

They are stored in binary form in your column. The format is only applied when you display the value and thus convert it to a character literal. That is the work of the client application you use to display the values. SQL*Plus uses the NLS settings, other SQL tools might use a different configuration.

于 2012-11-18T15:31:35.047 回答
0

All customers:

SELECT CUSTID
FROM BILLING
WHERE (SYSDATE - Billing_Date) >= 60
;

The following statement should update all the customer records where the difference is 60 days and over. Also, have added a clause to check if the service is already not set to CUT previously, so you don't end up updating the same records everytime.

UPDATE CUST_TABLE
SET SERVICE = 'CUT'
WHERE CustID in ( SELECT CustID
                   FROM BILLING_TABLE
                   WHERE (Sysdate - Billing_Date) >= 60
                 )
  AND NVL(SERVICE, 'X') != 'CUT'
;
于 2018-06-14T15:02:55.250 回答