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

从两个TIMESTAMP中获取时间差(秒)的更多相关文章

  1. C#中获取时间差

    /// <summary> /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. /// </summary> /// <param name=&q ...

  2. java程序中获取kerberos登陆hadoop

    本文由作者周梁伟授权网易云社区发布. 一般我们在使用kbs登陆hadoop服务时都直接在shell中调用kinit命令来获取凭证,这种方式简单直接,只要获取一次凭证之后都可以在该会话过程中重复访问.但 ...

  3. nodejs中获取时间戳、时间差

    Nodejs中获取时间戳的方法有很多种,例如: new Date().getTime() Date.now() process.uptime() process.hrtime() 平时想获取一个时间戳 ...

  4. PHP从mysqli中获取的资源$result是不是不能while($row = $result->fetch_assoc())这样两次?【坑】

    PHP从mysqli中获取的资源$result是不是不能while($row = $result->fetch_assoc())这样两次? 因为我这样做,结果后面的查询结果就无法显示了,目前尚不 ...

  5. C# 获取时间差(几天前,几小时前,几分钟前,几秒前)

    #region 获取时间差string GetTime(BsonString getTime) /// <summary> /// 获取时间差 /// </summary> / ...

  6. strus2中获取表单数据 两种方式 属性驱动 和模型驱动

    strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值 * 如果一个属性在对象栈,在页面 ...

  7. javascript 常见数组操作( 1、数组整体元素修改 2、 数组筛选 3、jquery 元素转数组 4、获取两个数组中相同部分或者不同部分 5、数组去重并倒序排序 6、数组排序 7、数组截取slice 8、数组插入、删除splice(需明确位置) 9、数组遍历 10、jQuery根据元素值删除数组元素的方)

    主要内容: 1.数组整体元素修改 2. 数组筛选 3.jquery 元素转数组 4.获取两个数组中相同部分或者不同部分 5.数组去重并倒序排序 6.数组排序 7.数组截取slice 8.数组插入.删除 ...

  8. java中获取两个时间中的每一天

    引入下面方法即可: /** * 获取两个时间中的每一天 * @param bigtimeStr 开始时间 yyyy-MM-dd * @param endTimeStr 结束时间 yyyy-MM-dd ...

  9. php获取两个数组相同的元素(交集)以及比较两个数组中不同的元素(差集)

    (一)php获取两个数组相同元素 array  array_intersect(array  $array1, array $array2, [, array $...]) array  array_ ...

随机推荐

  1. 如何关闭WordPress后台的主题、插件、版本更新通知?

    由于WordPress 更新速度非常快,不论是主题 插件或是版本,每个月少说要执行个好几次,因为更新快,所以WordPress后台加入了更新通知,提醒使用者有新版本了,可以进行插件.主题或是系统更新, ...

  2. vue引入自己写的js文件

    话不多说,直接上代码呀~ 先来个结构图: 中规中矩的vue-cli就写了一个自己的js文件 那么我想要引入到vue组件里. 1.首先写我的js文件 2.引入到vue组件!!!一定要用{}把方法名拿过来 ...

  3. ASP.NET MVC 3和Razor中的@helper

    ASP.NET MVC 3支持一项名为“Razor”的新视图引擎选项(除了继续支持/加强现有的.aspx视图引擎外).当编写一个视图模板时,Razor将所需的字符和击键数减少到最小,并保证一个快速.通 ...

  4. Could not apply the stored configuration for monitors

    在用户目录下$user.home/.config/monitors.xml,要解决上面的问题,最简单的办法就是删除这个monitors.xml文件,重启一下电脑

  5. Codeforces Round #436 (Div. 2) E. Fire(dp 记录路径)

    E. Fire time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  6. 使用matplotlib绘图(三)之饼图

    # 使用matplotlib绘制饼图 import numpy as np import matplotlib.pyplot as plt # 设置全局字体 plt.rcParams['font.sa ...

  7. bzoj1116 [POI2008]CLO 边双联通分量

    只需对每个联通块的$dfs$树检查有没有返租边即可 复杂度$O(n + m)$ #include <cstdio> #include <cstring> using names ...

  8. 整数求和 Exercise07_21

    import java.util.Scanner; public class Exercise07_21 { /** * @param 冰樱梦 * 时间:2018年12月 * 题目:整数求和 */ p ...

  9. mysql性能测试

    mysqlslap mysql自带的工具使用非常方面: 使用语法如下: # mysqlslap [options] 常用参数 [options] 详细说明: --auto-generate-sql, ...

  10. Codeforces Beta Round #10 B. Cinema Cashier 暴力

    B. Cinema Cashier 题目连接: http://www.codeforces.com/contest/10/problem/B Description All cinema halls ...