05.11.2015 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 12 ■ DATATYPES 537<br />

ops$tkyte@ORA10G> !oerr ora 30079<br />

30079, 00000, "cannot alter database timezone when database has<br />

TIMESTAMP WITH LOCAL TIME ZONE columns"<br />

// *Cause: An attempt was made to alter database timezone with<br />

// TIMESTAMP WITH LOCAL TIME ZONE column in the database.<br />

// *Action: Either do not alter database timezone or first drop all the<br />

// TIMESTAMP WITH LOCAL TIME ZONE columns.<br />

The reason is that if you were able to change the database’s time zone, you would have to<br />

rewrite every single table with a TIMESTAMP WITH LOCAL TIME ZONE—their current values would<br />

be wrong given the new time zone!<br />

INTERVAL Type<br />

We briefly saw INTERVAL type used in the previous section. It is a way to represent a duration of<br />

time or an interval of time. We’ll discuss two INTERVAL types in this section: the YEAR TO MONTH<br />

type, which is capable of storing a duration of time specified in years <strong>and</strong> months, <strong>and</strong> the<br />

DATE TO SECOND type, which is capable of storing a duration of time in days, hours, minutes,<br />

<strong>and</strong> seconds (including fractional seconds).<br />

Before we get into the specifics of the two INTERVAL types, I’d like to look at the EXTRACT<br />

built-in function, which can be very useful when working with this type. The EXTRACT built-in<br />

function works on TIMESTAMPs <strong>and</strong> INTERVALs, <strong>and</strong> returns various bits of information from<br />

them, such as the time zone from a TIMESTAMP or the hours/days/minutes from an INTERVAL.<br />

Using the previous example, where we got the INTERVAL of 380 days, 10 hours, 20 minutes, <strong>and</strong><br />

29.878 seconds<br />

ops$tkyte@ORA10G> select dt2-dt1<br />

2 from (select to_timestamp('29-feb-2000 01:02:03.122000',<br />

3 'dd-mon-yyyy hh24:mi:ss.ff') dt1,<br />

4 to_timestamp('15-mar-2001 11:22:33.000000',<br />

5 'dd-mon-yyyy hh24:mi:ss.ff') dt2<br />

6 from dual )<br />

7 /<br />

DT2-DT1<br />

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

+000000380 10:20:29.878000000<br />

we can use EXTRACT to see how easy it is to pull out each bit of information:<br />

ops$tkyte@ORA10G> select extract( day from dt2-dt1 ) day,<br />

2 extract( hour from dt2-dt1 ) hour,<br />

3 extract( minute from dt2-dt1 ) minute,<br />

4 extract( second from dt2-dt1 ) second<br />

5 from (select to_timestamp('29-feb-2000 01:02:03.122000',<br />

6 'dd-mon-yyyy hh24:mi:ss.ff') dt1,<br />

7 to_timestamp('15-mar-2001 11:22:33.000000',<br />

8 'dd-mon-yyyy hh24:mi:ss.ff') dt2

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

Saved successfully!

Ooh no, something went wrong!