oracle 查询时间跨度一年,关于时间跨度比较的OVERLAPS函数
我曾經在我的博客里介紹了如何使用工作區管理器(workspace)來管理時態。但必須首先配置好工作區管理器。
由于blogdriver空間有問題,所以計劃把博客遷到ITPUB。
SQL>SELECT *
2FROM dual
3WHERE (add_months(sysdate, -6), DATE '2008-08-08')
4OVERLAPS
5(sysdate - 180, interval '2' YEAR);
D
-
X
The OVERLAPS predicate tests
whether two “events” cover a common point in time or not, and has the form.:
(expression,
expression) OVERLAPS (expression, expression)
Each of the two
“events” specified on either side of the OVERLAPS keyword is a period of time
between two specified points on the timeline. The two points can be specified
as a pair of datetime values or as one datetime value and an INTERVAL offset.
Each “event” is
defined by a two expressions constituting a row value expression having two
columns.
The first
column in each row value expression must be a DATE, TIME or TIMESTAMP and the
value in the first column of the first “event” must be comparable.
The second
column in each row value expression may be either a DATE, TIME or TIMESTAMP
that is comparable with the value in the first column or an INTERVAL with a
precision that allows it to be added to the value in the first column.
The value in
the first column of each row value expression defines one of the points on the
timeline for the event.
If the value in
the second column of the row value expression is a datetime, it defines the
other point on the timeline for the event.
If the value in
the second column of the row value expression is an INTERVAL, the other point
on the timeline for the event is defined by adding the values in the two column
of the row value to expression together.
The NULL value
is assumed to be a point that is infinitely late in time.
Either of the
two points may be the earlier point in time.
If the value in
the first column of the row value expression is the NULL value, then this is
assumed to be the later point in time.
從這里,我們可以看出OVERLAPS有兩種用法:
除了本文開始提到的例子外,還有:
SQL> select * from dual
2where(date '2007-01-01',
interval '5' year)
3overlaps (date '2005-01-01',
interval '10' year);
D
-
X
比較的只能是日期,而不是數字,例如:
SQL> select * from dual
2where(1,5)
3overlaps (3,8) ;
where(1,5)
*
ERROR at line 2:
ORA-00932: inconsistent datatypes
由于這個函數是ORACLE的未公開的,所以不宜在產品系統中使用。一個Kevin Meade給出的替代方法:
where (s2 < e1 and e2 > s1) or (s1 < e2 and e1 > s2)
這個函數最早出現在David Aldridge’s的
超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生總結
以上是生活随笔為你收集整理的oracle 查询时间跨度一年,关于时间跨度比较的OVERLAPS函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: php同时上传多个数据,怎样一次上传多个
- 下一篇: oracle查询表字段sql语句怎么写,