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.

522<br />

CHAPTER 12 ■ DATATYPES<br />

DATE Type<br />

The DATE type is a fixed-width 7-byte date/time datatype. It will always contain the seven<br />

attributes of the century, the year within the century, the month, the day of the month, the<br />

hour, the minute, <strong>and</strong> the second. <strong>Oracle</strong> uses an internal format to represent that information,<br />

so it is not really storing 20, 05, 06, 25, 12, 01, 00 for June 25, 2005, at 12:01:00. Using the<br />

built-in DUMP function, we can see what <strong>Oracle</strong> really stores:<br />

ops$tkyte@ORA10G> create table t ( x date );<br />

Table created.<br />

ops$tkyte@ORA10G> insert into t (x) values<br />

2 ( to_date( '25-jun-2005 12:01:00',<br />

3 'dd-mon-yyyy hh24:mi:ss' ) );<br />

1 row created.<br />

ops$tkyte@ORA10G> select x, dump(x,10) d from t;<br />

X D<br />

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

25-JUN-05 Typ=12 Len=7: 120,105,6,25,13,2,1<br />

The century <strong>and</strong> year bytes (the 120,105 in the DUMP output) are stored in an excess-100<br />

notation. You would have to subtract 100 from them to determine the correct century <strong>and</strong><br />

year. The reason for the excess-100 notation is support of BC <strong>and</strong> AD dates. If you subtract<br />

100 from the century byte <strong>and</strong> get a negative number, it is a BC date, for example:<br />

ops$tkyte@ORA10G> insert into t (x) values<br />

2 ( to_date( '01-jan-4712bc',<br />

3 'dd-mon-yyyybc hh24:mi:ss' ) );<br />

1 row created.<br />

ops$tkyte@ORA10G> select x, dump(x,10) d from t;<br />

X D<br />

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

25-JUN-05 Typ=12 Len=7: 120,105,6,25,13,2,1<br />

01-JAN-12 Typ=12 Len=7: 53,88,1,1,1,1,1<br />

So, when we insert 01-JAN-4712BC, the century byte is 53 <strong>and</strong> 53 – 100 = –47, the century<br />

we inserted. Because it is negative, we know that it is a BC date. This storage format also allows<br />

the dates to be naturally sortable in a binary sense. Since 4712 BC is “less than” 4710 BC, we’d<br />

like a binary representation that supports that. By dumping those two dates, we can see that<br />

01-JAN-4710BC is “larger” than the same day in 4712 BC, so they will sort <strong>and</strong> compare nicely:<br />

ops$tkyte@ORA10G> insert into t (x) values<br />

2 ( to_date( '01-jan-4710bc',<br />

3 'dd-mon-yyyybc hh24:mi:ss' ) );<br />

1 row created.

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

Saved successfully!

Ooh no, something went wrong!