Oracle中的日期减法问题

时间:2013-04-10 12:23:45

标签: sql oracle datetime date-arithmetic

我在Oracle数据库上做日期替换时遇到了一些麻烦。

我有一个问题:

select 
status,
id, 
to_char(creationdatetime,'yyyy/mm/dd hh24:mm:ss') as Creation_Time,
to_char(lastmodificationdatetime,'yyyy/mm/dd hh24:mm:ss') as Last_Mod_Time,
substr(lastmodificationdatetime - creationdatetime,1,30)*24 as Time_Between,
--trunc((((86400*(lastmodificationdatetime - creationdatetime))/60)/60)/24) "Days",
--trunc(((86400*(lastmodificationdatetime - creationdatetime))/60)/60)-24*(trunc((((86400*(lastmodificationdatetime - creationdatetime))/60)/60)/24)) "Hrs",
--trunc((86400*(lastmodificationdatetime - creationdatetime))/60)-60*(trunc(((86400*(lastmodificationdatetime - creationdatetime))/60)/60)) "Min",
--trunc(86400*(lastmodificationdatetime - creationdatetime))-60*(trunc((86400*(lastmodificationdatetime - creationdatetime))/60)) "Sec"
from 
table
where
Status='Completed' or Status='Cancelled';

(trunc是检查计算日期的其他方式)

然后我得到结果:

Status       ID CreationDate    Lastmodificationdate    Time_Between                     Days   Hours   Minutes Seconds

Completed   id1 2013/03/25 12:03:14 2013/03/25 13:03:17 1,78416666666666666666666666648    0    1   47  3
Completed   id2 2013/03/26 09:03:22 2013/03/26 09:03:28 0,45166666666666666666666666656    0    0   27  5
Cancelled   is3 2012/12/19 17:12:50 2012/12/19 19:12:10 1,52222222222222222222222222208    0    1   31  19
Cancelled   id4 2012/12/19 18:12:13 2012/12/19 19:12:23 0,65277777777777777777777777768    0    0   39  10

正如我们所看到的,日期被错误地减去,当我复制日期并使用双重减去它们时:

select (to_date('2013/03/25 13:03:17', 'yyyy/MM/dd HH24:MI:SS') - 
to_date('2013/03/25 12:03:14', 'yyyy/MM/dd HH24:MI:SS'))
from dual;

我得到了正确的结果......我不知道最近会发生什么...... 非常感谢任何帮助。

1 个答案:

答案 0 :(得分:8)

您正在以不正确的格式显示日期。

mm是月份,而不是分钟。所以你在时间显示中重复这个月。

您想:'yyyy/mm/dd hh24:mi:ss'(请注意格式掩码中的mi

相关问题