Oracle下日期偽列巧用
阿新 • • 發佈:2018-11-16
以“天”為單位
select to_char(min_date+rownum-1,'yyyy-mm-dd') datestr
from
(
select to_date('2011-06-07','yyyy-mm-dd') max_date,to_date('2011-06-01','yyyy-mm-dd') min_date
from dual
)
connect by rownum<=(max_date-min_date)
以“月”為單位
select to_char(add_months(to_date('2011-06-01','yyyy-mm-dd'),rownum-1),'yyyy-mm-dd') datestr from dual connect by rownum<=months_between(to_date('2011-06-01','yyyy-mm-dd'),to_date('2011-01-01','yyyy-mm-dd'))
以“季度”為單位
select to_char(add_months(to_date('2011-01-01','yyyy-mm-dd'),(rownum-1)*3),'yyyy-MM') datestr
from dual
connect by rownum<=(months_between(to_date('2011-10-01','yyyy-mm-dd'),to_date('2011-01-01','yyyy-mm-dd'))/3)
以“年”為單位
select to_number(substr('2009-01-01',0,4))+rownum-1 datestr from dual connect by rownum<=to_number(substr('2011-01-01',0,4))-to_number(substr('2009-01-01',0,4))
------------------------------------------------------------------------------------------------------