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 523<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 />

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

The month <strong>and</strong> day bytes, the next two fields, are stored naturally, without any modification.<br />

So, June 25 used a month byte of 6 <strong>and</strong> a day byte of 25. The hour, minute, <strong>and</strong> second<br />

fields are stored in excess-1 notation, meaning we must subtract 1 from each component to<br />

see what time it really was. Hence midnight is represented as 1,1,1 in the date field.<br />

This 7-byte format is naturally sortable, as you have seen—it is a 7-byte field that can be<br />

sorted in a binary fashion from small to larger (or vice versa) very efficiently. Additionally, its<br />

structure allows for easy truncation, without converting the date into some other format. For<br />

example, truncating the date we just stored, 25-JUN-2005 12:01:00, to the day (remove the<br />

hours, minutes, seconds) is very straightforward. Just set the trailing three bytes to 1,1,1 <strong>and</strong><br />

the time component is as good as erased. Consider a fresh table, T, with the following inserts:<br />

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

Table created.<br />

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

2 ( 'orig',<br />

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

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

1 row created.<br />

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

2 select 'minute', trunc(x,'mi') from t<br />

3 union all<br />

4 select 'day', trunc(x,'dd') from t<br />

5 union all<br />

6 select 'month', trunc(x,'mm') from t<br />

7 union all<br />

8 select 'year', trunc(x,'y') from t<br />

9 /<br />

4 rows created.<br />

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

WHAT X D<br />

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

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

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

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

month 01-JUN-05 Typ=12 Len=7: 120,105,6,1,1,1,1<br />

year 01-JAN-05 Typ=12 Len=7: 120,105,1,1,1,1,1

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

Saved successfully!

Ooh no, something went wrong!