Oracle 构建定制 期间划分段 [类似期间表]

SQL> Select
  2              To_char(Sysdate,'YYYY')||Lpad(Rownum,2,0) As 期间,
  3              To_Date(To_char(Sysdate,'YYYY')||Lpad(Rownum,2,0)||'01','YYYY-MM-DD') As 开始时间 ,
  4               Add_Months(To_Date(To_char(Sysdate,'YYYY')||Lpad(Rownum,2,0)||'01','YYYY-MM-DD'),1)-1 As 结束时间
  5  From Dual
  6  Connect By Level <=12
  7  /
 
期间   开始时间 结束时间
-------- ----------- -----------
201001 2010-01-01 2010-01-31
201002 2010-02-01 2010-02-28
201003 2010-03-01 2010-03-31
201004 2010-04-01 2010-04-30
201005 2010-05-01 2010-05-31
201006 2010-06-01 2010-06-30
201007 2010-07-01 2010-07-31
201008 2010-08-01 2010-08-31
201009 2010-09-01 2010-09-30
201010 2010-10-01 2010-10-31
201011 2010-11-01 2010-11-30
201012 2010-12-01 2010-12-31
 
12 rows selected

 

 

--一个年份期间
Select
              To_Char(Sysdate,'YYYY')||Lpad(Rownum,2,0) As 期间,
              To_Date(To_char(Sysdate,'YYYY')||Lpad(Rownum,2,0)||'01','YYYY-MM-DD') As 开始时间 ,
              Last_Day(To_Date(To_char(Sysdate,'YYYY')||Lpad(Rownum,2,0)||'01','YYYY-MM-DD'))  As 结束时间
  From Dual
  Connect By Level<=12
  --多个期间 方法1[效率高些]
  Select
                 To_Char(Add_Months(To_Date('1990-01', 'yyyy-mm') - 1, Rownum),'YYYYMM'),
                 Trunc(Add_Months(To_Date('1990-01', 'yyyy-mm') - 1, Rownum), 'mm') 开始时间,
                 Add_Months(To_Date('1990-01', 'yyyy-mm') - 1, Rownum) 结束时间
  From Dual
  Connect By Rownum <= (12 * 1011)
  --多个期间 方法2
  Select      To_Char(Dt, 'yyyymm') 期间,
                  To_Char(Dt, 'yyyy-mm-dd') 开始时间,
                  To_Char(Last_Day(Dt), 'yyyy-mm-dd') 结束时间
  From (
  Select Add_Months(To_Date('19900101', 'yyyymmdd'), Level - 1) Dt
               From Dual Connect By Level <= (3000 - 1990 + 1) * 12
  )

你可能感兴趣的:(oracle,sql,Date,2010)