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

10.2.4.4 Formatting intervals for display

So far in this section on interval conversion, we've relied on Oracle's implicit conversion mechanism to format interval values for display. You may be thinking that you could useTO_CHAR in conjunction with a format mask to gain more control over how an interval is displayed. Amazingly enough, you'd be wrong! TO_CHAR is completely useless when it comes to formatting interval values. For example:

DECLARE

A INTERVAL YEAR TO MONTH;

BEGIN

A := INTERVAL '40-3' YEAR TO MONTH;

DBMS_OUTPUT.PUT_LINE(TO_CHAR(A,'YY "Years" and MM "Months"'));

END;

The output is:

+000040-03

Frankly, we don't understand what the people at Oracle were thinking when they decided against adding interval support to the TO_CHAR function.

If you're not satisfied with the default conversion of intervals to character strings, you can use the EXTRACT function (new in Oracle9i):

DECLARE

A INTERVAL YEAR TO MONTH;

BEGIN

A := INTERVAL '40-3' YEAR TO MONTH;

DBMS_OUTPUT.PUT_LINE(

EXTRACT(YEAR FROM A) || ' Years and '

|| EXTRACT(MONTH FROM A) || ' Months'

);

END;

The output is:

40 Years and 3 Months

Using EXTRACT is certainly not as convenient as using TO_CHAR, but the EXTRACT approach, described in the next section, has the advantage of actually working.

10.2.5 The cast and extract Functions

The CAST and EXTRACT functions are ANSI-standard functions (from SQL/92 forward) that are newly supported in Oracle. CAST made its appearance in Oracle8 as a mechanism for explicitly identifying collection types, and it was enhanced in Oracle8i to enable conversion between built-in datatypes. With respect to date and time, you can use CAST to convert datetime values to and from character strings. The EXTRACT function is new in Oracle9i, and allows you to pluck an individual datetime element from a datetime or interval value.

10.2.5.1 The cast function

With respect to date and time, you can use the CAST function as follows:

  • To convert a character string into a datetime value

  • To convert a datetime value into a character string

  • To convert one datetime type (e.g., DATE) into another (e.g., TIMESTAMP)

When used to convert datetimes to and from character strings, CAST respects the following NLS parameter settings:

NLS_DATE_FORMAT

When casting to or from a DATE

NLS_TIMESTAMP_FORMAT

When casting to or from a TIMESTAMP or a TIMESTAMP WITH LOCAL TIME ZONE

NLS_TIMESTAMP_TZ_FORMAT

When casting to or from a TIMESTAMP WITH TIME ZONE

The following example shows a representative of each type of CAST that is relevant when working with datetimes:

DECLARE

a TIMESTAMP WITH TIME ZONE;

b VARCHAR2(40);

c TIMESTAMP WITH LOCAL TIME ZONE;

BEGIN

a := CAST ('24-Feb-2002 09.00.00.00 PM US/Eastern'

AS TIMESTAMP WITH TIME ZONE);

b := CAST (a AS VARCHAR2);

c := CAST (a AS TIMESTAMP WITH LOCAL TIME ZONE);

DBMS_OUTPUT.PUT_LINE(a);

DBMS_OUTPUT.PUT_LINE(b);

DBMS_OUTPUT.PUT_LINE(c);

END;

The output is:

24-FEB-02 09.00.00.000000 PM US/EASTERN

24-FEB-02 09.00.00.000000 PM US/EASTERN

24-FEB-02 09.00.00.000000 PM

This example generates a TIMESTAMP WITH TIME ZONE from a character string, converts that value to a VARCHAR2, and finally converts it to a TIMESTAMP WITH LOCAL TIME ZONE. Note that no time zone conversion is done when the value is CAST from TIMESTAMP WITH TIME ZONE to TIMESTAMP WITH LOCAL TIME ZONE. In that case, the time zone information is lost.

In a SQL statement, you can specify the size of a datatype in a CAST, as in CAST (x AS VARCHAR2(40)). However, PL/SQL does not allow you to specify the size of the target datatype.

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