2013-04-10 73 views
1

我在Oracle数据库上做日期替换时遇到了一些麻烦。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; 

我得到正确的结果...我不知道发生了什么事... 希望有任何帮助。

+0

什么呢'SUBSTR(lastmodificationdatetime - creationdatetime,1,30)* 24'?为什么你不只是简单化他们,并与24乘以获得小时? – 2013-04-10 12:29:01

+0

您的“select .. from dual”与您的Time_Between计算不同。 – Rene 2013-04-10 12:30:56

回答

8

您显示的格式不正确。

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

你想:'yyyy/mm/dd hh24:mi:ss'(注意格式面具mi

+1

omg,即时通讯如此愚蠢...... thx – Iscario 2013-04-10 12:44:07