0

这是一个正确且有效的声明:

SELECT purch_id AS PURCH_ID
       ,TO_DATE('25-DEC-12') - purch_date AS DAYS_TO_CHRISTMAS
FROM CS260USER.candy_purchase;

我想修改它,而不是在表达式末尾硬编码 2012(或 12),我希望它抓住当前的系统年份并在最后拍打那个傻瓜。

想法?

4

1 回答 1

1
SELECT purch_id AS PURCH_ID,
       (trunc(sysdate,'yyyy') +
          interval '11' month +
          interval '24' day) - purch_date AS DAYS_TO_CHRISTMAS
  FROM CS260USER.candy_purchase;

将工作。你也可以通过字符串操作来做到这一点

SELECT purch_id AS PURCH_ID,
       to_date( '12-25-' || to_char(sysdate,'yyyy'), 'mm-dd-yyyy') 
         - purch_date AS DAYS_TO_CHRISTMAS
  FROM CS260USER.candy_purchase;

我更喜欢对日期进行日期操作,并对字符串进行字符串操作,尽管这里的字符串操作可能更清楚。

于 2012-09-17T04:24:23.300 回答