博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
从两个TIMESTAMP中获取时间差(秒)
阅读量:6942 次
发布时间:2019-06-27

本文共 2704 字,大约阅读时间需要 9 分钟。

When you subtract two variables of type TIMESTAMP, you get an INTERVAL DAY TO SECOND which includes a number of milliseconds and/or microseconds depending on the platform. If the database is running on Windows, systimestamp will generally have milliseconds. If the database is running on Unix, systimestamp will generally have microseconds.
  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
You can use the EXTRACT function to extract the individual elements of an INTERVAL DAY TO SECOND
SQL> ed
Wrote file afiedt.buf
select extract( day from diff ) days,
    extract( hour from diff ) hours,
    extract( minute from diff ) minutes,
    extract( second from diff ) seconds
 from (select systimestamp - to_timestamp( '2012-07-23', 'yyyy-mm-dd' ) diff
 from dual)
SQL> /
      DAYS      HOURS    MINUTES    SECONDS
---------- ---------- ---------- ----------
         0         14         55     37.936
You can then convert each of those components into milliseconds and add them up
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
Normally, however, it is more useful to have either the INTERVAL DAY TO SECOND representation or to have separate columns for hours, minutes, seconds, etc. rather than computing the total number of milliseconds between two TIMESTAMP values.
Subtraction between timestamps returns an INTERVAL datatype. You can use the EXTRACT function to return various parts of an interval eg select extract(hour from (timestamp '2009-12-31 14:00:00' - timestamp '2009-12-31 12:15:00')) hr from dual; Note: That only shows the HOUR part, so if the difference is 1 day and 1 hour, this will show 1 not 25. –  Gary Myers Jul 8 '09 at 22:42
Another answer:
SQL> @id8
SQL> drop   table holder ;
Table dropped.
SQL> create table holder (
  2  beg_date timestamp,
  3  end_date timestamp)
  4  /
Table created.
SQL> INSERT INTO HOLDER VALUES(to_timestamp('2009-07-16:19:00:01.50','YYYY-MM-DD:HH24:MI:SS.FF'),
  2                        to_timestamp('2009-08-17:20:00','YYYY-MM-DD:HH24:MI'));
1 row created.
SQL> COMMIT;
Commit complete.

SQL>

SELECT EXTRACT (DAY    FROM (END_DATE-BEG_DATE))*24*60*60+
    EXTRACT (HOUR   FROM (END_DATE-BEG_DATE))*60*60+
    EXTRACT (MINUTE FROM (END_DATE-BEG_DATE))*60+
    EXTRACT (SECOND FROM (END_DATE-BEG_DATE)) DELTA
FROM holder
     DELTA
----------
 2768398.5

转载地址:http://qianl.baihongyu.com/

你可能感兴趣的文章
Clipboard with Custom Clipboard Formats - Delphi
查看>>
[Step By Step]SAP HANA PAL 异态检测算法Anomaly Detection实现例程ANOMALYDETECTION
查看>>
linux上配置boost手记
查看>>
IIS状态监测(如果状态错误则重启IIS)
查看>>
PostgreSQL中,database,schema,table之间关系
查看>>
12个球一个天平,现知道只有一个和其它的重量不同,问怎样称才能用三次就找到那个球(13个呢?)...
查看>>
HDU 2364 (记忆化BFS搜索)
查看>>
两个实用的方法从Base64字符串生成RSAPublicKey及RSAPrivatekey
查看>>
常用验证数字的正则表达式
查看>>
kafka入门:简介、使用场景、设计原理、主要配置及集群搭建(转)[收藏]
查看>>
java读取excel文件数据
查看>>
Java的RMI远程方法调用实现和应用
查看>>
Linux 上使用 Gmail SMTP 服务器发送邮件通知
查看>>
Dell vsotro 14 3000系列从win10重装win7
查看>>
说说$POST 、$HTTP_RAW_POST_DATA、php://input三者之间的区别
查看>>
(一) Qt Model/View 的简单说明
查看>>
制作自己的MVC框架(三)——应用
查看>>
青云指南
查看>>
thinkphp 3.2 多表查询 group
查看>>
C#.NET 无法直接启动带有类库输出类型的项目怎么办
查看>>