Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Скачиваний:
37
Добавлен:
16.04.2013
Размер:
5.97 Mб
Скачать

Overview of Predefined PL/SQL Datatypes

query cannot take any BOOLEAN parameters. Neither can built-in SQL functions such as TO_CHAR; to represent BOOLEAN values in output, you must use IF-THEN or CASE constructs to translate BOOLEAN values into some other type, such as 0 or 1, 'Y' or 'N', 'true' or 'false', and so on.

PL/SQL Date, Time, and Interval Types

The datatypes in this section let you store and manipulate dates, times, and intervals (periods of time). A variable that has a date/time datatype holds values called datetimes; a variable that has an interval datatype holds values called intervals. A datetime or interval consists of fields, which determine its value. The following list shows the valid values for each field:

Field Name

Valid Datetime Values

Valid Interval Values

 

 

 

YEAR

-4712 to 9999 (excluding year 0)

Any nonzero integer

MONTH

01 to 12

0 to 11

DAY

01 to 31 (limited by the values of

Any nonzero integer

 

MONTH and YEAR, according to the

 

 

rules of the calendar for the

 

 

locale)

 

HOUR

00 to 23

MINUTE

00 to 59

SECOND

00 to 59.9(n), where 9(n) is the

 

precision of time fractional

 

seconds

TIMEZONE_HOUR

-12 to 14 (range accommodates

 

daylight savings time changes)

TIMEZONE_MINUTE

00 to 59

TIMEZONE_REGION

Found in the view V$TIMEZONE_

 

NAMES

TIMEZONE_ABBR

Found in the view V$TIMEZONE_

 

NAMES

0 to 23

0 to 59

0 to 59.9(n), where 9(n) is the precision of interval fractional seconds

Not applicable

Not applicable

Not applicable

Not applicable

Except for TIMESTAMP WITH LOCAL TIMEZONE, these types are all part of the SQL92 standard. For information about datetime and interval format models, literals, time-zone names, and SQL functions, see Oracle Database SQL Reference.

DATE

You use the DATE datatype to store fixed-length datetimes, which include the time of day in seconds since midnight. The date portion defaults to the first day of the current month; the time portion defaults to midnight. The date function SYSDATE returns the current date and time.

Tips:

To compare dates for equality, regardless of the time portion of each date, use the function result TRUNC(date_variable) in comparisons, GROUP BY operations, and so on.

To find just the time portion of a DATE variable, subtract the date portion: date_ variable - TRUNC(date_variable).

3-12 PL/SQL User's Guide and Reference

Overview of Predefined PL/SQL Datatypes

Valid dates range from January 1, 4712 BC to December 31, 9999 AD. A Julian date is the number of days since January 1, 4712 BC. Julian dates allow continuous dating from a common reference. You can use the date format model 'J' with the date functions TO_DATE and TO_CHAR to convert between DATE values and their Julian equivalents.

In date expressions, PL/SQL automatically converts character values in the default date format to DATE values. The default date format is set by the Oracle initialization parameter NLS_DATE_FORMAT. For example, the default might be 'DD-MON-YY', which includes a two-digit number for the day of the month, an abbreviation of the month name, and the last two digits of the year.

You can add and subtract dates. In arithmetic expressions, PL/SQL interprets integer literals as days. For instance, SYSDATE + 1 signifies the same time tomorrow.

TIMESTAMP

The datatype TIMESTAMP, which extends the datatype DATE, stores the year, month, day, hour, minute, and second. The syntax is:

TIMESTAMP[(precision)]

where the optional parameter precision specifies the number of digits in the fractional part of the seconds field. You cannot use a symbolic constant or variable to specify the precision; you must use an integer literal in the range 0 .. 9. The default is 6.

The default timestamp format is set by the Oracle initialization parameter NLS_ TIMESTAMP_FORMAT.

In the following example, you declare a variable of type TIMESTAMP, then assign a literal value to it:

DECLARE

checkout TIMESTAMP(3); BEGIN

checkout := '1999-06-22 07:48:53.275';

...

END;

In this example, the fractional part of the seconds field is 0.275.

TIMESTAMP WITH TIME ZONE

The datatype TIMESTAMP WITH TIME ZONE, which extends the datatype TIMESTAMP, includes a time-zone displacement. The time-zone displacement is the difference (in hours and minutes) between local time and Coordinated Universal Time (UTC)—formerly Greenwich Mean Time. The syntax is:

TIMESTAMP[(precision)] WITH TIME ZONE

where the optional parameter precision specifies the number of digits in the fractional part of the seconds field. You cannot use a symbolic constant or variable to specify the precision; you must use an integer literal in the range 0 .. 9. The default is 6.

The default timestamp with time zone format is set by the Oracle initialization parameter NLS_TIMESTAMP_TZ_FORMAT.

In the following example, you declare a variable of type TIMESTAMP WITH TIME ZONE, then assign a literal value to it:

DECLARE

logoff TIMESTAMP(3) WITH TIME ZONE;

PL/SQL Datatypes 3-13

Overview of Predefined PL/SQL Datatypes

BEGIN

logoff := '1999-10-31 09:42:37.114 +02:00';

...

END;

In this example, the time-zone displacement is +02:00.

You can also specify the time zone by using a symbolic name. The specification can include a long form such as 'US/Pacific', an abbreviation such as 'PDT', or a combination. For example, the following literals all represent the same time. The third form is most reliable because it specifies the rules to follow at the point when switching to daylight savings time.

TIMESTAMP '1999-04-15 8:00:00 -8:00'

TIMESTAMP '1999-04-15 8:00:00 US/Pacific'

TIMESTAMP '1999-10-31 01:30:00 US/Pacific PDT'

You can find the available names for time zones in the TIMEZONE_REGION and TIMEZONE_ABBR columns of the V$TIMEZONE_NAMES data dictionary view.

Two TIMESTAMP WITH TIME ZONE values are considered identical if they represent the same instant in UTC, regardless of their time-zone displacements. For example, the following two values are considered identical because, in UTC, 8:00 AM Pacific Standard Time is the same as 11:00 AM Eastern Standard Time:

'1999-08-29 08:00:00 -8:00' '1999-08-29 11:00:00 -5:00'

TIMESTAMP WITH LOCAL TIME ZONE

The datatype TIMESTAMP WITH LOCAL TIME ZONE, which extends the datatype TIMESTAMP, includes a time-zone displacement. The time-zone displacement is the difference (in hours and minutes) between local time and Coordinated Universal Time (UTC)—formerly Greenwich Mean Time. You can also use named time zones, as with

TIMESTAMP WITH TIME ZONE.

The syntax is

TIMESTAMP[(precision)] WITH LOCAL TIME ZONE

where the optional parameter precision specifies the number of digits in the fractional part of the seconds field. You cannot use a symbolic constant or variable to specify the precision; you must use an integer literal in the range 0 .. 9. The default is 6.

This datatype differs from TIMESTAMP WITH TIME ZONE in that when you insert a value into a database column, the value is normalized to the database time zone, and the time-zone displacement is not stored in the column. When you retrieve the value, Oracle returns it in your local session time zone.

In the following example, you declare a variable of type TIMESTAMP WITH LOCAL TIME ZONE:

DECLARE

logoff TIMESTAMP(3) WITH LOCAL TIME ZONE; BEGIN

...

END;

You cannot assign literal values to a variable of this type.

3-14 PL/SQL User's Guide and Reference

Overview of Predefined PL/SQL Datatypes

INTERVAL YEAR TO MONTH

You use the datatype INTERVAL YEAR TO MONTH to store and manipulate intervals of years and months. The syntax is:

INTERVAL YEAR[(precision)] TO MONTH

where precision specifies the number of digits in the years field. You cannot use a symbolic constant or variable to specify the precision; you must use an integer literal in the range 0 .. 4. The default is 2.

In the following example, you declare a variable of type INTERVAL YEAR TO MONTH, then assign a value of 101 years and 3 months to it:

DECLARE

lifetime INTERVAL YEAR(3) TO MONTH; BEGIN

lifetime := INTERVAL '101-3' YEAR TO MONTH; -- interval literal lifetime := '101-3'; -- implicit conversion from character type lifetime := INTERVAL '101' YEAR; -- Can specify just the years lifetime := INTERVAL '3' MONTH; -- Can specify just the months

...

END;

INTERVAL DAY TO SECOND

You use the datatype INTERVAL DAY TO SECOND to store and manipulate intervals of days, hours, minutes, and seconds. The syntax is:

INTERVAL DAY[(leading_precision)] TO SECOND[(fractional_seconds_precision)]

where leading_precision and fractional_seconds_precision specify the number of digits in the days field and seconds field, respectively. In both cases, you cannot use a symbolic constant or variable to specify the precision; you must use an integer literal in the range 0 .. 9. The defaults are 2 and 6, respectively.

In the following example, you declare a variable of type INTERVAL DAY TO SECOND:

DECLARE

lag_time INTERVAL DAY(3) TO SECOND(3); BEGIN

IF lag_time > INTERVAL '6' DAY THEN ...

...

END;

Datetime and Interval Arithmetic

PL/SQL lets you construct datetime and interval expressions. The following list shows the operators that you can use in such expressions:

Operand 1

Operator

Operand 2

Result Type

 

 

 

 

datetime

+

interval

datetime

datetime

-

interval

datetime

interval

+

datetime

datetime

datetime

-

datetime

interval

interval

+

interval

interval

interval

-

interval

interval

PL/SQL Datatypes 3-15

Тут вы можете оставить комментарий к выбранному абзацу или сообщить об ошибке.

Оставленные комментарии видны всем.

Соседние файлы в папке Oracle 10g