简体   繁体   中英

How to write mysql TIMESTAMPDIFF function in Oracle sql query

I have query in Mysql which return minutes using TIMESTAMPDIFF in table. But now i have migrated my data to Oracle. So i want to use the same query to get the TIMESTAMPDIFF in a table in Oracle. Oracle also dont support NOW() function in mysql. The PROCESS_START_DATE column in query have data which contains date and time. I tried EXTRACT function in oraclebut did not work. Here is my query :

    select * from(
    select trunc(abs(to_date('27/01/2015 08:00:00','dd/mm/yyyy hh:mi:ss') - PMS.PROCESS_START_DATE)*24*60),PM.NAME,PM.ENABLED
    from PROCESS_MONITOR_STATISTIC PMS
    JOIN PROCESS_MONITOR PM ON PM.ID=PMS.PROCESS_MONITOR_ID   
    WHERE PM.ENABLED=1 AND PM.NAME= 'WORKFLOWENGINE1'
    order by PMS.PROCESS_START_DATE desc
) 
where ROWNUM = 1

You can do something like this:

--in case you are working with dates
select trunc(abs(to_date('26/01/2015 08:00:00','dd/mm/yyyy hh:mi:ss') - sysdate)*24*60) from dual; 

This represent difference in minutes between a date and now(sysdate) with dates.

--timestamp case
select abs(
extract (day from diff)*24*60 + extract (hour from diff)*60 + extract (minute from diff)) from
(select to_timestamp('27/01/2015 09:07:00','dd/mm/yyyy hh:mi:ss') - systimestamp diff from dual);

This represent difference in minutes between a date and now(systimestamp) with timestamp.

Edit:

This query calculate minutes in a year:

select 365*24*60 from dual -- this returns 525600

This is your query. i change the time. Check that the difference between these dates is one year and five minutes

select trunc(abs((to_date('26/01/14 09:00:00','dd/mm/yy hh24:mi:ss')-
to_date('26/01/2015 09:05:01','dd/mm/yyyy hh24:mi:ss'))*24*60)) from dual;

So, when run this query result is 525605 , five minutes more than a year. So it looks to be working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM