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

10.2.5.2 The extract function

The EXTRACT function is an ANSI-standard function used to extract date components from a datetime value. Use the following format when invoking EXTRACT:

EXTRACT (component_name, FROM {datetime | interval})

In this syntax, component_name is the name of a datetime element listed in Table 10-3. Component names are not case-sensitive. Replace datetime or interval with a valid datetime or interval value. The function's return type depends on the component you are extracting.

Table 10-3. Datetime component names for use with EXTRACT

Component name

Return datatype

YEAR

NUMBER

MONTH

NUMBER

DAY

NUMBER

HOUR

NUMBER

MINUTE

NUMBER

SECOND

NUMBER

TIMEZONE_HOUR

NUMBER

TIMEZONE_MINUTE

NUMBER

TIMEZONE_REGION

VARCHAR2

TIMEZONE_ABBR

VARCHAR2

The following example shows EXTRACT being used to check whether the current month is November:

BEGIN

IF EXTRACT (MONTH FROM SYSDATE) = 11 THEN

DBMS_OUTPUT.PUT_LINE('It is November');

ELSE

DBMS_OUTPUT.PUT_LINE('It is not November');

END IF;

END;

Use EXTRACT when you need to use a datetime element to control program flow, as in this example, or when you need a datetime element as a numeric value. Don't be tempted to use EXTRACT as a means of converting a datetime value to a character string, as in the following example:

BEGIN

DBMS_OUTPUT.PUT_LINE('Today''s date is: ' ||

EXTRACT(MONTH FROM SYSDATE) || '/' ||

EXTRACT(DAY FROM SYSDATE) || '/' ||

EXTRACT(YEAR FROM SYSDATE));

END;

The output is:

Today's date is: 2/24/2002

Look at how clumsy this is! EXTRACT is not designed to convert a datetime to a character string, as it returns mainly numeric values, not character string values. In this example, all those numbers are implicitly converted to character strings. Not only that, EXTRACT returns only numbers, not day names or month names or any of the other good things TO_CHAR provides. Use TO_CHAR to convert datetimes to character strings. Use EXTRACT when you just need to programmatically look at a value.

10.3 Date/Time Arithmetic

PL/SQL supports arithmetic involving datetime values, numeric values, and interval values. For example, you can add a given number of days to a date, or you can subtract two dates in order to determine the interval of time between them. Date arithmetic prior to the release of Oracle9i is a very simple topic. Oracle9i's addition of INTERVAL types makes it much more complex.

10.3.1 Traditional Date Arithmetic

Prior to Oracle9i, date arithmetic meant doing one of two things:

  • Adding or subtracting numeric values, representing some number of days, from DATE values.

  • Subtracting one DATE from another in order to determine the number of days between them.

In Oracle9i this functionality is still available, but only with the DATE datatype.

You also could (and still can) make use of date functions such as ADD_MONTHS and MONTHS_BETWEEN. We discuss these later in Section 10.4.

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