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

Formats<br />

I am not going to attempt to cover all of the DATE, TIMESTAMP, <strong>and</strong> INTERVAL formats here. That<br />

is well covered in the <strong>Oracle</strong> SQL Reference manual, which is freely available to all. A wealth of<br />

formats is available to you, <strong>and</strong> a good underst<strong>and</strong>ing of what they are is vital. It is strongly<br />

recommended that you investigate them.<br />

I’d like to discuss what the formats do here, as there are a great many misconceptions surrounding<br />

this topic. The formats are used for two things:<br />

• To format the data on the way out of the database in a style that pleases you<br />

• To tell the database how to convert an input string into a DATE, TIMESTAMP, or INTERVAL<br />

And that is all. The common misconception I’ve observed over the years is that the format<br />

used somehow affects what is stored on disk <strong>and</strong> how the data is actually saved. The format<br />

has no effect at all on how the data is stored. The format is only used to convert the single binary<br />

format used to store a DATE into a string or to convert a string into the single binary format that<br />

is used to store a DATE. The same is true for TIMESTAMPs <strong>and</strong> INTERVALs.<br />

My advice on formats is simply this: use them. Use them when you send a string to the<br />

database that represents a DATE, TIMESTAMP, or INTERVAL. Do not rely on default date formats—<br />

defaults can <strong>and</strong> probably will at some point in the future be changed by someone. If you rely<br />

on a default date format <strong>and</strong> it changes, your application may be negatively affected. It might<br />

raise an error back to the end user if the date cannot be converted or, worse still, it might<br />

silently insert the wrong data. Consider the follow INSERT statement, which relies on a default<br />

date mask:<br />

Insert into t ( date_column ) values ( '01/02/03' );<br />

Suppose the application was relying on a default date mask of DD/MM/YY to be in place.<br />

That would be February 1, 2003 (assuming that code was executed after the year 2000, but<br />

we’ll visit the implications of that in a moment). Now, say someone decides the correct <strong>and</strong><br />

proper date format is MM/DD/YY. All of a sudden, that previous date changes to January 2, 2003.<br />

Or someone decides YY/MM/DD is right, <strong>and</strong> now you have February 3, 2001. In short, without a<br />

date format to accompany that date string, there are many ways to interpret it. That INSERT<br />

statement should be<br />

Insert into t ( date_column ) values ( to_date( '01/02/03', 'DD/MM/YY' ) );<br />

And if you want my opinion, it has to be<br />

Insert into t ( date_column ) values ( to_date( '01/02/2003', 'DD/MM/YYYY' ) );<br />

That is, it must use a four-character year. Not too long ago, our industry learned the hard<br />

way how much time <strong>and</strong> effort was spent remedying software that attempted to “save” 2 bytes—<br />

we seem to have lost that lesson over time. There is no excuse in the year 2005 <strong>and</strong> beyond not<br />

to use a four-character year!<br />

This same discussion applies to data leaving the database. If you execute SELECT DATE_<br />

COLUMN FROM T <strong>and</strong> fetch that column into a string in your application, then you should apply<br />

an explicit date format to it. Whatever format your application is expecting should be explicitly<br />

applied there. Otherwise, at some point in the future when someone changes the default<br />

date format, your application may break or behave incorrectly.<br />

Next, let’s look at the datatypes themselves in more detail.

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

Saved successfully!

Ooh no, something went wrong!