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 531<br />

DT<br />

TS<br />

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

28-jun-2005 10:45:20 28-JUN-05 10.45.20.744866000 AM<br />

ops$tkyte@ORA10G> select dump(ts,16) dump from t;<br />

DUMP<br />

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

Typ=180 Len=11: 78,69,6,1c,b,2e,15,2c,65,c0,d0<br />

ops$tkyte@ORA10G> select to_number('2c65c0d0','xxxxxxxx') from dual;<br />

TO_NUMBER('2C65C0D0','XXXXXXXX')<br />

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

744866000<br />

We can see the fractional seconds that were stored are there in the last 4 bytes. We used<br />

the DUMP function to inspect the data in HEX this time (base 16) so we could easily convert the<br />

4 bytes into the decimal representation.<br />

Adding Time to or Subtracting Time from a TIMESTAMP<br />

The same techniques we applied to DATE for date arithmetic work with a TIMESTAMP, but the<br />

TIMESTAMP will be converted into a DATE in many cases using the preceding techniques, for<br />

example:<br />

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

Session altered.<br />

ops$tkyte@ORA10G> select systimestamp ts, systimestamp+1 dt<br />

2 from dual;<br />

TS<br />

DT<br />

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

28-JUN-05 11.04.49.833097 AM -04:00 29-jun-2005 11:04:49<br />

Note that adding 1 did, in fact, advance the SYSTIMESTAMP by a day, but the fractional seconds<br />

are gone, as would be the time zone information. This is where using INTERVALs will be<br />

more important:<br />

ops$tkyte@ORA10G> select systimestamp ts, systimestamp +numtodsinterval(1,'day') dt<br />

2 from dual;<br />

TS<br />

DT<br />

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

28-JUN-05 11.08.03.958866 AM -04:00 29-JUN-05 11.08.03.958866000 AM -04:00<br />

Using the function that returns an INTERVAL type preserved the fidelity of the TIMESTAMP.<br />

You will need to exercise caution when using TIMESTAMPs, to avoid the implicit conversions.

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

Saved successfully!

Ooh no, something went wrong!