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.

520<br />

CHAPTER 12 ■ DATATYPES<br />

TABLE_OWN TABLE PARTIT HIGH_VALUE<br />

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

OPS$TKYTE T PART1 TO_DATE(' 2003-03-13 00:00:00'<br />

, 'SYYYY-MM-DD HH24:MI:SS', 'N<br />

LS_CALENDAR=GREGORIAN')<br />

OPS$TKYTE T<br />

PART2 TO_DATE(' 2003-03-14 00:00:00'<br />

, 'SYYYY-MM-DD HH24:MI:SS', 'N<br />

LS_CALENDAR=GREGORIAN')<br />

Using this same technique—that of processing the result of a query that returns a single<br />

row with a single LONG column in a function—you can implement your own INSTR, LIKE, <strong>and</strong> so<br />

on as needed.<br />

This implementation works well on the LONG type, but it will not work on LONG RAW types.<br />

LONG RAWs are not piecewise accessible (there is no COLUMN_VALUE_LONG_RAW function in DBMS_SQL).<br />

Fortunately, this is not too serious of a restriction since LONG RAWs are not used in the dictionary<br />

<strong>and</strong> the need to “substring” so you can search on it is rare. If you do have a need to do so,<br />

however, you will not use PL/SQL unless the LONG RAW is 32KB or less, as there is simply no<br />

method for dealing with LONG RAWs over 32KB in PL/SQL itself. Java, C, C++, Visual Basic, or<br />

some other language would have to be used.<br />

Another approach is to temporarily convert the LONG or LONG RAW into a CLOB or BLOB using<br />

the TO_LOB built-in function <strong>and</strong> a global temporary table. Your PL/SQL procedure could be as<br />

follows:<br />

Insert into global_temp_table ( blob_column )<br />

select to_lob(long_raw_column) from t where...<br />

This would work well in an application that occasionally needs to work with a single<br />

LONG RAW value. You would not want to continuously do that, however, due to the amount of<br />

work involved. If you find yourself needing to resort to this technique frequently, you should<br />

definitely convert the LONG RAW to a BLOB once <strong>and</strong> be done with it.<br />

DATE, TIMESTAMP, <strong>and</strong> INTERVAL Types<br />

The native <strong>Oracle</strong> datatypes of DATE, TIMESTAMP, <strong>and</strong> INTERVAL are closely related. The DATE <strong>and</strong><br />

TIMESTAMP types store fixed date/times with varying degrees of precision. The INTERVAL type is<br />

used to store an amount of time, such as “8 hours” or “30 days,” easily. The result of subtracting<br />

two dates might be an interval; for example, the result of adding an interval of 8 hours to a<br />

TIMESTAMP results in a new TIMESTAMP that is 8 hours later.<br />

The DATE datatype has been part of <strong>Oracle</strong> for many releases—as far back as my experience<br />

with <strong>Oracle</strong> goes, which means at least back to version 5 <strong>and</strong> probably before. The TIMESTAMP<br />

<strong>and</strong> INTERVAL types are relative newcomers to the scene by comparison, as they were introduced<br />

with <strong>Oracle</strong>9i Release 1. For this simple reason, you will find the DATE datatype to be the<br />

most prevalent type for storing date/time information. But many new applications are using<br />

the TIMESTAMP type for two reasons: it has support for fractions of seconds, while the DATE type<br />

does not; <strong>and</strong> it has support for time zones, something the DATE type also does not have.<br />

We’ll take a look at each type after discussing DATE/TIMESTAMP formats <strong>and</strong> their uses.

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

Saved successfully!

Ooh no, something went wrong!