从两个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 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中获取时间差(秒),布布扣,bubuko.com

时间: 2024-10-18 10:38:23

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

C#中获取时间差

1 /// <summary> 2 /// 已重载.计算两个日期的时间间隔,返回的是时间间隔的日期差的绝对值. 3 /// </summary> 4 /// <param name="DateTime1">第一个日期和时间</param> 5 /// <param name="DateTime2">第二个日期和时间</param> 6 /// <returns></return

如何在不同编程语言中获取现在的Unix时间戳(Unix timestamp)?

Unix时间戳(Unix timestamp),或称Unix时间(Unix time).POSIX时间(POSIX time),是一种时间表示方式,定义为从格林威治时间1970年01月01日00时00分00秒起至现在的总秒数.Unix时间戳不仅被使用在Unix 系统.类Unix系统中,也在许多其他操作系统中被广告采用. 目前相当一部分操作系统使用32位二进制数字表示时间.此类系统的Unix时间戳最多可以使用到格林威治时间2038年01月19日03时14分07秒(二进制:01111111 1111

获取两个字符串中最大相同子串

2.获取两个字符串中最大相同子串.第一个动作:将短的那个串进行长度一次递减的子串打印. "cvhellobnmtanop" "andefc" 思路: 1,将短的那个子串按照长度递减的方式获取到. 2,将每获取到的子串去长串中判断是否包含,如果包含,已经找到! package tan; class Test { public static String getMaxSubString(String s1,String s2) { String max = "

给定两个字符串,获取两个字符串中最大相同的子串

1 package weekpratisce; 2 3 ///给定两个字符串,获取两个字符串中最大相同的子串 4 public class Demo9 { 5 public static void main(String[] args) { 6 String xx = "aaaaaaaaaaddddddd", yy = "45ddddda"; 7 String str = getMaxsubstring(xx, yy); 8 System.out.println(s

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

strus2中获取表单数据 两种方式 属性驱动 和模型驱动 属性驱动 /** * 当前请求的action在栈顶,ss是栈顶的元素,所以可以利用setValue方法赋值* 如果一个属性在对象栈,在页面上可以根据name属性进行回显*/ /** * 属性驱动实现的条件:* 1.当前请求的action在栈顶,所以action中的属性就暴漏出来了* 2.获取页面上表单的元素,整合成一个map * 3.调用setValue方法赋值*/ 1 package cn.itcast.struts2.sh; 2 3

QT中获取选中的radioButton的两种方法(动态取得控件的objectName之后,对名字进行比较)

QT中获取选中的radioButton的两种方法 QT中要获取radioButton组中被选中的那个按钮,可以采用两种如下两种办法进行: 方法一:采用对象名称进行获取 代码: 1 QRadioButton* pbtn = qobject_cast<QRadioButton*>(ui->BG->checkedButton()); 2 QString name = pbtn->objectName(); 3 if(!QString::compare(name, "rad

60秒验证码倒计时js代码 js样式代码 方块上下左右随机移动(定时器) js中获取元素的方法 js中表单提交

60秒验证码倒计时js代码 <script type="text/javascript"> var countdown=60; function settime(val) { if (countdown == 0) { //removeAttribute() 方法删除指定的属性. disabled属性规定应该禁用 input 元素. val.removeAttribute("disabled"); val.value="免费获取验证码"

获取两个字符串中最大的相同子串

public class 获取两个字符串中最大的相同子串 { public static void main(String[] args) { String a="abcwerthelloadcedf"; String b="cdhelloesadcedf"; String c=getSonString(a,b); System.out.println(c); } private static String getSonString(String a, String

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

1 #region 获取时间差string GetTime(BsonString getTime) 2 /// <summary> 3 /// 获取时间差 4 /// </summary> 5 /// <param name="getTime">数据库时间</param> 6 /// <returns>时间差</returns> 7 private string GetTime(BsonString getTime