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

10.3.1.2 Computing the difference between two dates

You can't add two DATE values. If you think about that logically, it makes sense. What does it mean, for example, to add 15-Nov-1961 to 18-Jun-1961? On the other hand, you can subtract one DATE value from another, resulting in the difference in days between the two dates. For example:

BEGIN

DBMS_OUTPUT.PUT_LINE (

TO_DATE('15-Nov-1961','dd-Mon-yyyy')

- TO_DATE('18-Jun-1961','dd-Mon-yyyy')

);

END;

The output is:

150

From this, you can see that 18-Jun-1961 falls 150 days prior to 15-Nov-1961. Be careful with the results of subtracting two DATE values, because if any time-of-day components are involved, you'll get a fractional result. Look at the same subtraction again; this time we've added some carefully chosen times:

BEGIN

DBMS_OUTPUT.PUT_LINE (

TO_DATE('15-Nov-1961 12:01 am','dd-Mon-yyyy hh:mi am')

- TO_DATE('18-Jun-1961 11:59 pm','dd-Mon-yyyy hh:mi am')

);

END;

The output is:

149.001388888888888888888888888888888889

If you're interested in the number of calendar days between the two dates shown in this example, the result is clearly not correct. It is correct from the standpoint of measuring the number of 24-hour periods between the two DATE values, but most humans want to know that the two dates are 150 days apart, not that one DATE value is 149.00139 24-hour periods distant from the other. You can get correct results by applying the TRUNC function to each of the date values:

BEGIN

DBMS_OUTPUT.PUT_LINE (

TRUNC(TO_DATE('15-Nov-1961 12:01 am','dd-Mon-yyyy hh:mi am'))

- TRUNC(TO_DATE('18-Jun-1961 11:59 pm','dd-Mon-yyyy hh:mi am'))

);

END;

The output is:

150

TRUNC eliminates any time-of-day component from the two DATE values, making the resulting subtraction equivalent to the first example in this section, and the result is the number of calendar days between the two dates. TRUNC is a very useful function when working with DATEs, and is discussed in detail later in Section 10.4.

The result of subtracting one DATE from another is aNUMBER. The result of subtracting one TIMESTAMP type from another is an INTERVAL DAY TO SECOND.

10.3.2 Interval Arithmetic

Oracle9i introduces the concept of an INTERVAL datatype, leading to a whole new level of complexity when it comes to date arithmetic. But the concept of an interval isn't really new. If you've been doing date arithmetic in older releases of Oracle, then you've already been working with intervals. Date arithmetic involving numbers, as described in the previous section, is nothing more than date arithmetic using intervals expressed in terms of days and fractions of a day. Think back to the problem of adding one day to a date. In Oracle9i, you can do either of the following:

hire_date + 1;

hire_date + INTERVAL '1' DAY;

See, we've been working with intervals all along. Oracle has just now formalized the concept, and in the process has given us two more interval types to work with. Beginning in Oracle9i, you can express intervals in any of the following terms:

INTERVAL YEAR TO MONTH type

Allows us to express intervals in terms of years and months

INTERVAL DAY TO SECOND type

Allows us to express intervals in terms of days, hours, minutes, and seconds

Numeric values

Allow us to express intervals in terms of days and fractions of a day

With this in mind, date arithmetic in Oracle9i can be reduced to the following three types of operations:

  • Adding or subtracting an interval to or from a datetime value

  • Subtracting one datetime value from another in order to determine the interval between the two values

  • Adding or subtracting one interval to or from another interval

The only thing that's really new here is the terminology and the INTERVAL datatypes. Don't let these intimidate you. The first two bullets correspond to the first two bullets in the previous section; only the terminology has changed. Even the third bullet represents nothing new—we've always been able to add and subtract date intervals. It's just that pre-Oracle9i, that meant adding and subtracting numbers, whereas now, when working with the new TIMESTAMP family of types, you can add or subtract INTERVAL datatypes.

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