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

rekharaghuram
from rekharaghuram More from this publisher
05.11.2015 Views

CHAPTER 12 ■ DATATYPES 533 ops$tkyte@ORA10G> select numtoyminterval 2 (months_between(dt2,dt1),'month') 3 years_months, 4 dt2-(dt1 + numtoyminterval( trunc(months_between(dt2,dt1)),'month' )) 5 days_hours 6 from (select to_timestamp('29-feb-2000 01:02:03.122000', 7 'dd-mon-yyyy hh24:mi:ss.ff') dt1, 8 to_timestamp('15-mar-2001 11:22:33.000000', 9 'dd-mon-yyyy hh24:mi:ss.ff') dt2 10 from dual ) 11 / dt2-(dt1 + numtoyminterval( trunc(months_between(dt2,dt1)),'month' )) * ERROR at line 4: ORA-01839: date not valid for month specified I personally find this unacceptable. The fact is, though, that by the time you are displaying information with years and months, the fidelity of the TIMESTAMP is destroyed already. A year is not fixed in duration (it may be 365 or 366 days in length) and neither is a month. If you are displaying information with years and months, the loss of microseconds is not relevant; having the information displayed down to the second is more than sufficient at that point. TIMESTAMP WITH TIME ZONE Type The TIMESTAMP WITH TIME ZONE type inherits all of the qualities of the TIMESTAMP type and adds time zone support. The TIMESTAMP WITH TIME ZONE type consumes 13 bytes of storage, with the extra 2 bytes being used to preserve the time zone information. It differs from a TIMESTAMP structurally only by the addition of these 2 bytes: ops$tkyte@ORA10G> create table t 2 ( 3 ts timestamp, 4 ts_tz timestamp with time zone 5 ) 6 / Table created. ops$tkyte@ORA10G> insert into t ( ts, ts_tz ) 2 values ( systimestamp, systimestamp ); 1 row created. ops$tkyte@ORA10G> select * from t; TS TS_TZ ---------------------------- ----------------------------------- 28-JUN-05 01.45.08.087627 PM 28-JUN-05 01.45.08.087627 PM -04:00 ops$tkyte@ORA10G> select dump(ts), dump(ts_tz) from t;

534 CHAPTER 12 ■ DATATYPES DUMP(TS) ------------------------------------------------------------------------------- DUMP(TS_TZ) ------------------------------------------------------------------------------- Typ=180 Len=11: 120,105,6,28,14,46,9,5,57,20,248 Typ=181 Len=13: 120,105,6,28,18,46,9,5,57,20,248,16,60 As you can see, upon retrieval the default TIMESTAMP format included the time zone information (I was on East Coast US time during daylight saving time when this was executed). TIMESTAMP WITH TIME ZONEs store the data in whatever time zone was specified when the data was stored. The time zone becomes part of the data itself. Note how the TIMESTAMP WITH TIME ZONE field stored ...18,46,9... for the hour, minutes, and seconds (in excess-1 notation, so that is 17:45:08), whereas the TIMESTAMP field stored simply ...14,46,9..., which is 13:45:09—the exact time in the string we inserted. The TIMESTAMP WITH TIME ZONE had four hours added to it, in order to store in GMT (also known as UTC) time. The trailing 2 bytes are used upon retrieval to properly adjust the TIMESTAMP value. It is not my intention to cover all of the nuances of time zones here in this book; that is a topic well covered elsewhere. To that end, I’ll just point out that time zone support is more relevant in applications today than ever before. A decade ago, applications were not nearly as global as they are now. In the days before widespread Internet use, applications were many times distributed and decentralized, and the time zone was implicitly based on where the server was located. Today, with large centralized systems being used by people worldwide, the need to track and use time zones is very relevant. Before time zone support was built into a datatype, you had to store the DATE in one column and the time zone information in another, and then it was the application's job to apply functions to convert DATEs from one time zone to another. Now that is the job of the database, and it is able to store data in multiple time zones: ops$tkyte@ORA10G> create table t 2 ( ts1 timestamp with time zone, 3 ts2 timestamp with time zone 4 ) 5 / Table created. ops$tkyte@ORA10G> insert into t (ts1, ts2) 2 values ( timestamp'2005-06-05 17:02:32.212 US/Eastern', 3 timestamp'2005-06-05 17:02:32.212 US/Pacific' ); 1 row created. and perform correct TIMESTAMP arithmetic on them: ops$tkyte@ORA10G> select ts1-ts2 from t; TS1-TS2 --------------------------------------------------------------------------- -000000000 03:00:00.000000

534<br />

CHAPTER 12 ■ DATATYPES<br />

DUMP(TS)<br />

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

DUMP(TS_TZ)<br />

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

Typ=180 Len=11: 120,105,6,28,14,46,9,5,57,20,248<br />

Typ=181 Len=13: 120,105,6,28,18,46,9,5,57,20,248,16,60<br />

As you can see, upon retrieval the default TIMESTAMP format included the time zone information<br />

(I was on East Coast US time during daylight saving time when this was executed).<br />

TIMESTAMP WITH TIME ZONEs store the data in whatever time zone was specified when the<br />

data was stored. The time zone becomes part of the data itself. Note how the TIMESTAMP WITH<br />

TIME ZONE field stored ...18,46,9... for the hour, minutes, <strong>and</strong> seconds (in excess-1 notation,<br />

so that is 17:45:08), whereas the TIMESTAMP field stored simply ...14,46,9..., which is<br />

13:45:09—the exact time in the string we inserted. The TIMESTAMP WITH TIME ZONE had four<br />

hours added to it, in order to store in GMT (also known as UTC) time. The trailing 2 bytes are<br />

used upon retrieval to properly adjust the TIMESTAMP value.<br />

It is not my intention to cover all of the nuances of time zones here in this book; that is a<br />

topic well covered elsewhere. To that end, I’ll just point out that time zone support is more relevant<br />

in applications today than ever before. A decade ago, applications were not nearly as<br />

global as they are now. In the days before widespread Internet use, applications were many<br />

times distributed <strong>and</strong> decentralized, <strong>and</strong> the time zone was implicitly based on where the<br />

server was located. Today, with large centralized systems being used by people worldwide, the<br />

need to track <strong>and</strong> use time zones is very relevant.<br />

Before time zone support was built into a datatype, you had to store the DATE in one column<br />

<strong>and</strong> the time zone information in another, <strong>and</strong> then it was the application's job to apply<br />

functions to convert DATEs from one time zone to another. Now that is the job of the database,<br />

<strong>and</strong> it is able to store data in multiple time zones:<br />

ops$tkyte@ORA10G> create table t<br />

2 ( ts1 timestamp with time zone,<br />

3 ts2 timestamp with time zone<br />

4 )<br />

5 /<br />

Table created.<br />

ops$tkyte@ORA10G> insert into t (ts1, ts2)<br />

2 values ( timestamp'2005-06-05 17:02:32.212 US/Eastern',<br />

3 timestamp'2005-06-05 17:02:32.212 US/Pacific' );<br />

1 row created.<br />

<strong>and</strong> perform correct TIMESTAMP arithmetic on them:<br />

ops$tkyte@ORA10G> select ts1-ts2 from t;<br />

TS1-TS2<br />

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

-000000000 03:00:00.000000

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

Saved successfully!

Ooh no, something went wrong!