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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 19:49:48  来源:igfitidea点击:

oracle date sequence?

sqloracledateplsqlsequence

提问by Hoax

I have an oracle db and I need a table containing all the dates spanning 2 years; for example from 01/01/2011to 01/01/2013.

我有一个 oracle 数据库,我需要一个包含跨越 2 年的所有日期的表;例如从01/01/201101/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 levelin the connect byclause to fit your end point, and then plug it into an insertstatement.

如果您想要使用连续日期填充记录块,这很容易做到。以下查询生成十个日期。您需要做的就是调整种子日期以提供您的起点和子句中的levelinconnect 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