问题
i have some trouble doing dates substractions on Oracle database.
I have a query:
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 is to check other way of counting date)
Then I get results:
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
As we can see dates are substracted wrongly, when i copy dates and substracted them using dual:
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;
I get correct results... I have no idea whats going on... Would appreciate any help.
回答1:
You are displaying the dates with an incorrect format.
mm
is the month, not minutes. So you are repeating the month in the time display.
You want: 'yyyy/mm/dd hh24:mi:ss'
(note the mi
in the format mask)
来源:https://stackoverflow.com/questions/15925717/trouble-with-date-substraction-in-oracle