PL / SQL以不同格式提取日期之间的差异

时间:2017-09-07 15:25:28

标签: oracle date plsql extract date-difference

这是我尝试使用的查询:

select a.cr_mdt_abstractor_dt, b.created,
(trunc(a.CR_MDT_ABSTRACTOR_DT - b.created) || ' days and ' ||<br>
        to_char(to_date('01/01/2000', 'MM-DD-YYYY') + (a.CR_MDT_ABSTRACTOR_DT - b.created),
                'HH24:MI:SS'
               )
       ) as Diff
from CR_MDT a,
CR_MDT_VERIFY b
where a.CR_MDT_ID=b.FK_CR_MDT_ID

我得到的错误是:

  

ORA-00932:不一致的数据类型:预期NUMBER到第二天的间隔时间

b.created列的日期格式为09-MAR-17 10.52.23.195311 AM(TIMESTAMP) 和..cr_mdt_abstractor_dt的列在DATE。

我是否可以获得帮助以更正查询,以便将列b.created转换为格式以从a.cr_mdt_abstractor_dt中提取?

1 个答案:

答案 0 :(得分:1)

分别在TRUNCCR_MDT_ABSTRACTOR_DT)和TIMESTAMP上使用created

SELECT a.cr_mdt_abstractor_dt,
  b.created,
  TRUNC(a.CR_MDT_ABSTRACTOR_DT) - TRUNC(b.created)
  || ' days and '
  || TO_CHAR(to_date('01/01/2000', 'MM-DD-YYYY') + (a.CR_MDT_ABSTRACTOR_DT - b.created), 'HH24:MI:SS' ) AS Diff
FROM CR_MDT a,
  CR_MDT_VERIFY b
WHERE a.CR_MDT_ID=b.FK_CR_MDT_ID;

示例输出:

CREATED       DIFF
07-09-17    -182 days and 12:15:15