计算Oracle中两个时间戳之差(以毫秒为单位)

如何计算Oracle中两个时间戳之间的时间差(以毫秒为单位)?

当你减去两个types为TIMESTAMPvariables时,你将得到一个INTERVAL DAY TO SECOND ,其中包括几毫秒和/或几微秒,具体取决于平台。 如果数据库在Windows上运行, systimestamp通常会有毫秒。 如果数据库在Unix上运行, systimestamp通常会有微秒。

  1 select systimestamp - to_timestamp( '2012-07-23', 'yyyy-mm-dd' ) 2* from dual SQL> / SYSTIMESTAMP-TO_TIMESTAMP('2012-07-23','YYYY-MM-DD') --------------------------------------------------------------------------- +000000000 14:51:04.339000000 

您可以使用EXTRACTfunction提取INTERVAL DAY TO SECOND的各个元素

 SQL> ed Wrote file afiedt.buf 1 select extract( day from diff ) days, 2 extract( hour from diff ) hours, 3 extract( minute from diff ) minutes, 4 extract( second from diff ) seconds 5 from (select systimestamp - to_timestamp( '2012-07-23', 'yyyy-mm-dd' ) diff 6* from dual) SQL> / DAYS HOURS MINUTES SECONDS ---------- ---------- ---------- ---------- 0 14 55 37.936 

然后,您可以将这些组件中的每一个转换成毫秒并添加它们

 SQL> ed Wrote file afiedt.buf 1 select extract( day from diff )*24*60*60*1000 + 2 extract( hour from diff )*60*60*1000 + 3 extract( minute from diff )*60*1000 + 4 round(extract( second from diff )*1000) total_milliseconds 5 from (select systimestamp - to_timestamp( '2012-07-23', 'yyyy-mm-dd' ) diff 6* from dual) SQL> / TOTAL_MILLISECONDS ------------------ 53831842 

然而,通常情况下, INTERVAL DAY TO SECOND表示或者为几小时,几分钟,几秒钟等分开列是比较有用的,而不是计算两个TIMESTAMP值之间的总毫秒数。

这是一个存储过程来做到这一点:

 CREATE OR REPLACE function timestamp_diff(a timestamp, b timestamp) return number is begin return extract (day from (ab))*24*60*60 + extract (hour from (ab))*60*60+ extract (minute from (ab))*60+ extract (second from (ab)); end; / 

如果您还想打败那些否定他的工作的Oracle开发人员,

因为第一次比较时间戳应该采取每个人一个小时左右…

更简单的解决scheme

 SELECT numtodsinterval(date1-date2,'day') time_difference from dates; 

对于时间戳:

 SELECT (extract(DAY FROM time2-time1)*24*60*60)+ (extract(HOUR FROM time2-time1)*60*60)+ (extract(MINUTE FROM time2-time1)*60)+ extract(SECOND FROM time2-time1) into diff FROM dual; RETURN diff; 

从表中selectdate1 – (date2 – 1)* 24 * 60 * 60 * 1000;

最好使用这样的程序:

 CREATE OR REPLACE FUNCTION timestamp_diff ( start_time_in TIMESTAMP , end_time_in TIMESTAMP ) RETURN NUMBER AS l_days NUMBER; l_hours NUMBER; l_minutes NUMBER; l_seconds NUMBER; l_milliseconds NUMBER; BEGIN SELECT extract(DAY FROM end_time_in-start_time_in) , extract(HOUR FROM end_time_in-start_time_in) , extract(MINUTE FROM end_time_in-start_time_in) , extract(SECOND FROM end_time_in-start_time_in) INTO l_days, l_hours, l_minutes, l_seconds FROM dual; l_milliseconds := l_seconds*1000 + l_minutes*60*1000 + l_hours*60*60*1000 + l_days*24*60*60*1000; RETURN l_milliseconds; END; 

您可以通过以下方式查看:

 SELECT timestamp_diff (TO_TIMESTAMP('12.04.2017 12:00:00.00', 'DD.MM.YYYY HH24:MI:SS.FF'), TO_TIMESTAMP('12.04.2017 12:00:01.111', 'DD.MM.YYYY HH24:MI:SS.FF')) as milliseconds FROM DUAL; 

在其他格式之间正确传送的时间戳有可能会被误解。

这里是一个正确的工作示例,当从TableXYZ中考虑两个不同的date(Date2,Date1)时。

 SELECT ROUND (totalSeconds / (24 * 60 * 60), 1) TotalTimeSpendIn_DAYS, ROUND (totalSeconds / (60 * 60), 0) TotalTimeSpendIn_HOURS, ROUND (totalSeconds / 60) TotalTimeSpendIn_MINUTES, ROUND (totalSeconds) TotalTimeSpendIn_SECONDS FROM (SELECT ROUND ( EXTRACT (DAY FROM timeDiff) * 24 * 60 * 60 + EXTRACT (HOUR FROM timeDiff) * 60 * 60 + EXTRACT (MINUTE FROM timeDiff) * 60 + EXTRACT (SECOND FROM timeDiff)) totalSeconds, FROM (SELECT TO_TIMESTAMP ( TO_CHAR (Date2, 'yyyy-mm-dd HH24:mi:ss') - 'yyyy-mm-dd HH24:mi:ss'), TO_TIMESTAMP ( TO_CHAR (Date1, 'yyyy-mm-dd HH24:mi:ss'), 'yyyy-mm-dd HH24:mi:ss') timeDiff FROM TableXYZ)) 

以上一个语法错误,请在oracle上使用:

 SELECT ROUND (totalSeconds / (24 * 60 * 60), 1) TotalTimeSpendIn_DAYS, ROUND (totalSeconds / (60 * 60), 0) TotalTimeSpendIn_HOURS, ROUND (totalSeconds / 60) TotalTimeSpendIn_MINUTES, ROUND (totalSeconds) TotalTimeSpendIn_SECONDS FROM (SELECT ROUND ( EXTRACT (DAY FROM timeDiff) * 24 * 60 * 60 + EXTRACT (HOUR FROM timeDiff) * 60 * 60 + EXTRACT (MINUTE FROM timeDiff) * 60 + EXTRACT (SECOND FROM timeDiff)) totalSeconds FROM (SELECT TO_TIMESTAMP(TO_CHAR( date2 , 'yyyy-mm-dd HH24:mi:ss'), 'yyyy-mm-dd HH24:mi:ss') - TO_TIMESTAMP(TO_CHAR(date1, 'yyyy-mm-dd HH24:mi:ss'),'yyyy-mm-dd HH24:mi:ss') timeDiff FROM TABLENAME ) ); 

I)如果您需要计算两个时间戳列之间的经过时间,请尝试以下操作:

SELECT提取(从(end_timestamp – start_timestamp)开始)* 86400 +提取(从(end_timestamp – start_timestamp)开始的小时)* 3600 +提取(从(end_timestamp – start_timestamp)开始的分钟)* 60 +提取(从(end_timestamp – start_timestamp) FROM table_name

二)如果你想只显示字符格式的时差请试试这个:

SELECT to_char(end_timestamp – start_timestamp)FROM table_name