【问题标题】:How to perform arithmetic operation on Oracle timestamps (in SQL)?如何对 Oracle 时间戳执行算术运算(在 SQL 中)?
【发布时间】:2010-10-25 21:03:26
【问题描述】:

我需要选择一个在不同实体有效期中间有效的实体。

作为一个简化的例子,我想做这样的事情:

select 
    * 
from
    o1, o2
where
    o1.from < (o2.to - ((o2.to - o2.from) /2) )
    and
    o1.to > (o2.to - ((o2.to - o2.from) /2) )

假设 to 和 from 是时间戳,我如何在 SQL 中计算“(o2.to - ((o2.to - o2.from) /2) )”?

【问题讨论】:

  • 显然我的本地版本中有一些错字,感谢您的大开眼界。

标签: sql oracle timestamp


【解决方案1】:

你试过你写的东西了吗?看起来它应该对我有用:

(o2.to - o2.from)

这将为您提供部分天差,例如:

  1  select (trunc(sysdate) - trunc(sysdate+1))/2 from dual
  2*
SQL> /

(TRUNC(SYSDATE)-TRUNC(SYSDATE+1))/2
-----------------------------------
                                -.5

SQL>

那么如果你把那个结果加上原来的日期,你会得到中间点的日期和时间:

SQL> select to_char(a.d, 'YYYY-MON-DD HH24:MI')
  2  from
  3  (  select trunc(sysdate) + (trunc(sysdate+1) - trunc(sysdate))/2 d from dual
  4  ) a;

TO_CHAR(A.D,'YYYY
-----------------
2009-MAY-13 12:00

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-31
    • 1970-01-01
    • 1970-01-01
    • 2022-06-14
    • 1970-01-01
    • 2020-05-16
    • 1970-01-01
    • 2013-05-16
    相关资源
    最近更新 更多