首页 数据库 mysql教程 从两个TIMESTAMP中获取时间差(秒)

从两个TIMESTAMP中获取时间差(秒)

Jun 07, 2016 pm 03:56 PM
s timestamp 时间差 获取

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 h

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
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.能量晶体解释及其做什么(黄色晶体)
1 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

如何在Python中获取文件扩展名? 如何在Python中获取文件扩展名? Sep 08, 2023 pm 01:53 PM

如何在Python中获取文件扩展名?

谷歌安全码在哪里获取 谷歌安全码在哪里获取 Mar 30, 2024 am 11:11 AM

谷歌安全码在哪里获取

使用math.Max函数获取一组数中的最大值 使用math.Max函数获取一组数中的最大值 Jul 24, 2023 pm 01:24 PM

使用math.Max函数获取一组数中的最大值

php中计算日期相差多少天 php中计算日期相差多少天 Apr 09, 2024 pm 01:06 PM

php中计算日期相差多少天

如何在Java中获取LinkedHashSet的最后一个元素? 如何在Java中获取LinkedHashSet的最后一个元素? Aug 27, 2023 pm 08:45 PM

如何在Java中获取LinkedHashSet的最后一个元素?

Java程序获取给定文件的大小(以字节、千字节和兆字节为单位) Java程序获取给定文件的大小(以字节、千字节和兆字节为单位) Sep 06, 2023 am 10:13 AM

Java程序获取给定文件的大小(以字节、千字节和兆字节为单位)

简易JavaScript教程:获取HTTP状态码的方法 简易JavaScript教程:获取HTTP状态码的方法 Jan 05, 2024 pm 06:08 PM

简易JavaScript教程:获取HTTP状态码的方法

Mysql的timestamp时间戳2038问题怎么解决 Mysql的timestamp时间戳2038问题怎么解决 Jun 02, 2023 am 10:13 AM

Mysql的timestamp时间戳2038问题怎么解决

See all articles