2010-07-02 81 views

回答

4

這顯示瞭如何

with cc as(
select to_date('12-jan-1999') as cdate from dual union all 
select to_date('12-jan-1921') as cdate from dual union all 
select to_date('12-jan-1900') as cdate from dual union all 
select to_date('12-jan-2000') as cdate from dual union all 
select to_date('12-jan-2010') as cdate from dual 
) 
select to_date(to_char(cdate,'DD-MON') ||'-2011','DD-MON-YYYY') 
from cc 
where cdate < to_date('01-JAN-2010','DD-MON-YYYY') 
/
+0

我喜歡這個。我非常喜歡這個。給我幾分鐘來測試它。 – jake 2010-07-02 08:45:54

+1

這將在閏年每年有一天中斷。 ADD_MONTHS是一個更強大的解決方案。 – Allan 2010-07-02 17:30:59

3

1年= 12個月,因此減去12月:

select add_months(sysdate,-12) from dual 
1

這裏是如何做到這一點,因此使用ADD_MONTHS閏年工作。

with cc as( 
select to_date('12-jan-1999','dd-mon-yyyy') as cdate from dual union all 
select to_date('12-jan-1921','dd-mon-yyyy') as cdate from dual union all 
select to_date('29-feb-1904','dd-mon-yyyy') as cdate from dual union all 
select to_date('12-jan-2000','dd-mon-yyyy') as cdate from dual union all 
select to_date('12-jan-2010','dd-mon-yyyy') as cdate from dual 
) 
select add_months(cdate,(2011 - extract(year from cdate)) * 12) 
from cc 
where cdate < to_date('01-JAN-2010','DD-MON-YYYY'); 
相關問題