简体   繁体   English

如何在Oracle SQL查询中编写mysql TIMESTAMPDIFF函数

[英]How to write mysql TIMESTAMPDIFF function in Oracle sql query

I have query in Mysql which return minutes using TIMESTAMPDIFF in table. 我在Mysql中查询,该查询使用表中的TIMESTAMPDIFF返回分钟。 But now i have migrated my data to Oracle. 但是现在我已经将数据迁移到了Oracle。 So i want to use the same query to get the TIMESTAMPDIFF in a table in Oracle. 因此,我想使用相同的查询在Oracle表中获取TIMESTAMPDIFF。 Oracle also dont support NOW() function in mysql. Oracle在MySQL中也不支持NOW()函数。 The PROCESS_START_DATE column in query have data which contains date and time. 查询中的PROCESS_START_DATE列包含包含日期和时间的数据。 I tried EXTRACT function in oraclebut did not work. 我在oracle中尝试了EXTRACT函数,但是没有用。 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. 这表示日期与带有日期的now(sysdate)之间的分钟数差异。

--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. 这表示日期与带有时间戳的now(systimestamp)之间的分钟差。

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. 因此,运行此查询结果时为525605 ,比一年多五分钟。 So it looks to be working. 因此,它似乎正在工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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