oracle oracle日期序列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2085939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
oracle date sequence?
提问by Hoax
I have an oracle db and I need a table containing all the dates spanning 2 years;
for example from 01/01/2011
to 01/01/2013
.
我有一个 oracle 数据库,我需要一个包含跨越 2 年的所有日期的表;例如从01/01/2011
到01/01/2013
。
First I thought of a sequence but apparently the only supported type is number, so now I am looking for an efficient way to do this
首先我想到了一个序列,但显然唯一支持的类型是数字,所以现在我正在寻找一种有效的方法来做到这一点
cheers hoax
欢呼恶作剧
回答by APC
If what you want is to populate a block of records with sequential dates, that is easy enough to do. The following query generates ten dates. All you need to do is adjust the seed date to give you your starting point and the level
in the connect by
clause to fit your end point, and then plug it into an insert
statement.
如果您想要使用连续日期填充记录块,这很容易做到。以下查询生成十个日期。您需要做的就是调整种子日期以提供您的起点和子句中的level
inconnect by
以适应您的终点,然后将其插入insert
语句中。
SQL> select (trunc(sysdate, 'MM')-1) + level
2 from dual
3 connect by level <= 10
4 /
(TRUNC(SY
---------
01-JAN-10
02-JAN-10
03-JAN-10
04-JAN-10
05-JAN-10
06-JAN-10
07-JAN-10
08-JAN-10
09-JAN-10
10-JAN-10
10 rows selected.
SQL>
回答by R van Rijn
Say for example we have a table named: datums, with the column datum(date type) table contains:
例如,我们有一个名为:datums 的表,其中列 datum(date type) 表包含:
21-01-2010
22-01-2010
01-12-2009
06-10-2008
03-07-2007
then you could use:
那么你可以使用:
SELECT *
FROM datums
WHERE datum
BETWEEN to_date('01/01/2009','mm/dd/yyyy')
AND to_date('12/31/2010','mm/dd/yyyy')
result:
结果:
21-01-2010
22-01-2010
01-12-2009