05.11.2015 Views

Apress.Expert.Oracle.Database.Architecture.9i.and.10g.Programming.Techniques.and.Solutions.Sep.2005

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 12 ■ DATATYPES 527<br />

month has fewer than 31 days, ADD_MONTHS will return the last day of the next month. Additionally,<br />

adding one month to the last day of a month results in the last day of the next month. We<br />

see this when adding one month to a month with 30 or fewer days:<br />

ops$tkyte@ORA10G> alter session set nls_date_format = 'dd-mon-yyyy hh24:mi:ss';<br />

Session altered.<br />

ops$tkyte@ORA10G> select dt, add_months(dt,1)<br />

2 from (select to_date('29-feb-2000','dd-mon-yyyy') dt from dual )<br />

3 /<br />

DT<br />

ADD_MONTHS(DT,1)<br />

-------------------- --------------------<br />

29-feb-2000 00:00:00 31-mar-2000 00:00:00<br />

ops$tkyte@ORA10G> select dt, add_months(dt,1)<br />

2 from (select to_date('28-feb-2001','dd-mon-yyyy') dt from dual )<br />

3 /<br />

DT<br />

ADD_MONTHS(DT,1)<br />

-------------------- --------------------<br />

28-feb-2001 00:00:00 31-mar-2001 00:00:00<br />

ops$tkyte@ORA10G> select dt, add_months(dt,1)<br />

2 from (select to_date('30-jan-2001','dd-mon-yyyy') dt from dual )<br />

3 /<br />

DT<br />

ADD_MONTHS(DT,1)<br />

-------------------- --------------------<br />

30-jan-2001 00:00:00 28-feb-2001 00:00:00<br />

ops$tkyte@ORA10G> select dt, add_months(dt,1)<br />

2 from (select to_date('30-jan-2000','dd-mon-yyyy') dt from dual )<br />

3 /<br />

DT<br />

ADD_MONTHS(DT,1)<br />

-------------------- --------------------<br />

30-jan-2000 00:00:00 29-feb-2000 00:00:00<br />

See how the result of adding one month to February 29, 2000, results in March 31, 2000?<br />

February 29 was the last day of that month, so ADD_MONTHS returned the last day of the next<br />

month. Additionally, notice how adding one month to January 30, 2000 <strong>and</strong> 2001 results in the<br />

last day of February 2000 <strong>and</strong> 2001, respectively.<br />

If we compare this to how adding an interval would work, we see very different results:<br />

ops$tkyte@ORA10G> select dt, dt+numtoyminterval(1,'month')<br />

2 from (select to_date('29-feb-2000','dd-mon-yyyy') dt from dual )<br />

3 /<br />

DT<br />

DT+NUMTOYMINTERVAL(1<br />

-------------------- --------------------<br />

29-feb-2000 00:00:00 29-mar-2000 00:00:00

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!