Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Programming PL SQL.doc
Скачиваний:
2
Добавлен:
01.07.2025
Размер:
5.06 Mб
Скачать

Chapter 10. Dates and Timestamps

Most of our applications require the storage and manipulation of dates and times. Dates are quite complicated: not only are they highly formatted data, but there are myriad rules for determining valid values and valid calculations (leap days and years, national and company holidays, date ranges, etc.). Fortunately, the Oracle RDBMS and PL/SQL offer us lots of help in handling date information.

First of all, both the RDBMS and PL/SQL provide you with a set of true datetime datatypes that store both date and time information using a standard, internal format. No matter how you choose to represent datetime values when you enter them or display them, such values are represented in a consistent manner within PL/SQL and the RDBMS.

For any datetime value, Oracle stores some or all of the following information:

Year

Month

Day

Hour

Minute

Second

Fractional second

Time zone hour displacement

Time zone minute displacement

Time zone region name

Time zone abbreviation

Support for true datetime datatypes is only half the battle. You also need a language that can manipulate those values in a natural and intelligent manner—as actual dates and times. Oracle provides us with a comprehensive suite of functions with which to manipulate date and time information. Need toconvert a character string to a date? No problem. Oracle has you covered with the TO_DATE function, which can interpret and validate a variety of different date formats. Need to convert times between time zones? Again, Oracle has you covered—use the NEW_TIME function.

Not only do you have a rich set of built-in datetime functions to work with, you can perform certain types of datetime arithmetic directly on any datetime value. For example, computing the number of days between two dates is as simple as subtracting one from another.

With all this datetime power at your disposal, it's important to understand your choices. To that end, the next section discusses the various datetime datatypes available from PL/SQL. Next we talk about conversions, and show you how to get datetime values into and out of your datetime variables. In the remaining sections of the chapter, we describe the datetime functions that are available and show you how to work with and manipulate the various datetime datatypes that Oracle supports.

10.1 Date and Time Datatypes

It used to be that you didn't have any choices. Life then was simple. In the days of Oracle8i, Oracle8, and prior releases, if you needed to work with datetime values, you used the DATE datatype, end of story. With Oracle9i, however, Oracle introduced a slew of new datetime datatypes and related functionality.

Much of the new datetime functionality in Oracle9i focuses on the new TIMESTAMP and INTERVAL families of datatypes. Related to these are the new timestamp and interval expressions. Not only is this new functionality useful, it also brings Oracle into greater conformance with current ANSI/ISO SQL standards.

10.1.1 The date Datatype

DATE is the original datetime datatype supported by Oracle, and is the only one available through Oracle8i. An Oracle DATE stores the following information as a fixed-length, seven-byte value:

Year

Month

Day

Hour

Minute

Second

You cannot actually specify these internal values in an assignment statement. Instead you rely on implicit conversion of character and numeric values to an actual date, or explicit conversion with the TO_DATE function. See Section 10.2 later in this chapter.

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]