财政年度日期序列生成

时间:2015-04-27 11:03:17

标签: sql oracle plsql oracle11g

我有一个名为会计年度的表,其列为start_date,end_date(空表),我希望在每个会计年度之前插入记录直到2060年

FISCAL_YEAR开始dt是7月1日,结束日是明年6月31日

我尝试了什么

irb(main):019:0> date = '2015-04-27T20:00+00:00'
=> "2015-04-27T20:00+00:00"
irb(main):020:0> date = date.sub(/^(\d+)-(\d+)-(\d+)T.*$/,'\3-\2-\1');
irb(main):021:0* date
=> "27-04-2015"
irb(main):022:0>

我如何生成这个序列直到2060年

select  add_months(start_date ,-6),add_months(start_date ,6)-1 from (
select to_date('20000101','yyyymmdd') start_date from dual )
 basis 

2 个答案:

答案 0 :(得分:2)

SQL> select
  2    to_date('01-07-' || (1999 + rownum), 'dd.mm.yyyy') start_date,
  3    to_date('30-06-' || (2000 + rownum), 'dd.mm.yyyy') finish_date
  4  from dual
  5  connect by level <= 10;

START_DATE  FINISH_DATE
----------- -----------
01.07.2000  30.06.2001
01.07.2001  30.06.2002
01.07.2002  30.06.2003
01.07.2003  30.06.2004
01.07.2004  30.06.2005
01.07.2005  30.06.2006
01.07.2006  30.06.2007
01.07.2007  30.06.2008
01.07.2008  30.06.2009
01.07.2009  30.06.2010

10 rows selected

答案 1 :(得分:0)

你可以这样做:

SELECT 
   ADD_MONTHS(DATE '1999-07-01', 12*(LEVEL-1)) as fiscal_year_begin,
   ADD_MONTHS(DATE '1999-07-01', 12*LEVEL) - INTERVAL '1' DAY AS fiscal_year_end
FROM dual
CONNECT BY LEVEL < 60;

FISCAL_YEAR_BEGIN   FISCAL_YEAR_END
1999-07-01  2000-06-30
2000-07-01  2001-06-30
2001-07-01  2002-06-30
2002-07-01  2003-06-30
2003-07-01  2004-06-30
2004-07-01  2005-06-30
2005-07-01  2006-06-30
2006-07-01  2007-06-30
2007-07-01  2008-06-30
2008-07-01  2009-06-30
...