Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / SQL reference.pdf
Скачиваний:
24
Добавлен:
12.05.2015
Размер:
11.92 Mб
Скачать

CURRENT_DATE

CURRENT_DATE

Syntax current_date::=

CURRENT_DATE

Purpose

CURRENT_DATE returns the current date in the session time zone, in a value in the Gregorian calendar of datatype DATE.

Examples

The following example illustrates that CURRENT_DATE is sensitive to the session time zone:

ALTER SESSION SET TIME_ZONE = ’-5:0’;

ALTER SESSION SET NLS_DATE_FORMAT = ’DD-MON-YYYY HH24:MI:SS’;

SELECT SESSIONTIMEZONE, CURRENT_DATE FROM DUAL;

SESSIONTIMEZONE CURRENT_DATE

--------------- --------------------

-05:00 29-MAY-2000 13:14:03

ALTER SESSION SET TIME_ZONE = ’-8:0’;

SELECT SESSIONTIMEZONE, CURRENT_DATE FROM DUAL;

SESSIONTIMEZONE CURRENT_DATE

--------------- --------------------

-08:00 29-MAY-2000 10:14:33

Functions 6-49

CURRENT_TIMESTAMP

CURRENT_TIMESTAMP

Syntax current_timestamp::=

( precision )

CURRENT_TIMESTAMP

Purpose

CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of datatype TIMESTAMP WITH TIME ZONE. The time zone displacement reflects the current local time of the SQL session. If you omit precision, then the default is 6. The difference between this function and LOCALTIMESTAMP is that

CURRENT_TIMESTAMP returns a TIMESTAMP WITH TIME ZONE value while LOCALTIMESTAMP returns a TIMESTAMP value.

In the optional argument, precision specifies the fractional second precision of the time value returned.

Examples

The following example illustrates that CURRENT_TIMESTAMP is sensitive to the session time zone:

ALTER SESSION SET TIME_ZONE = ’-5:0’;

ALTER SESSION SET NLS_DATE_FORMAT = ’DD-MON-YYYY HH24:MI:SS’;

SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM DUAL;

SESSIONTIMEZONE CURRENT_TIMESTAMP

--------------- ---------------------------------------------------

-05:00 04-APR-00 01.17.56.917550 PM -05:00

ALTER SESSION SET TIME_ZONE = ’-8:0’;

SELECT SESSIONTIMEZONE, CURRENT_TIMESTAMP FROM DUAL;

SESSIONTIMEZONE CURRENT_TIMESTAMP

--------------- ----------------------------------------------------

-08:00 04-APR-00 10.18.21.366065 AM -08:00

If you use the CURRENT_TIMESTAMP with a format mask, take care that the format mask matches the value returned by the function. For example, consider the following table:

6-50 Oracle9i SQL Reference

DBTIMEZONE

CREATE TABLE current_test (col1 TIMESTAMP WITH TIME ZONE);

The following statement fails because the mask does not include the TIME ZONE portion of the type returned by the function:

INSERT INTO current_test VALUES

(TO_TIMESTAMP_TZ(CURRENT_TIMESTAMP, ’DD-MON-RR HH.MI.SSXFF PM’));

The following statement uses the correct format mask to match the return type of

CURRENT_TIMESTAMP:

INSERT INTO current_test VALUES (TO_TIMESTAMP_TZ (CURRENT_TIMESTAMP, ’DD-MON-RR HH.MI.SSXFF PM TZH:TZM’));

DBTIMEZONE

Syntax dbtimezone::=

DBTIMEZONE

Purpose

DBTIMEZONE returns the value of the database time zone. The return type is a time zone offset (a character type in the format ’[+|-]TZH:TZM’) or a time zone region name, depending on how the user specified the database time zone value in the most recent CREATE DATABASE or ALTER DATABASE statement.

Examples

The following example assumes that the database time zone is set to UTC time zone:

SELECT DBTIMEZONE FROM DUAL;

DBTIME

------

-08:00

Functions 6-51

DECODE

DECODE

Syntax decode::=

 

 

 

 

 

,

,

default

 

 

 

 

 

 

DECODE

(

expr

,

search

,

result

)

Purpose

DECODE compares expr to each search value one by one. If expr is equal to a search, then Oracle returns the corresponding result. If no match is found, then Oracle returns default. If default is omitted, then Oracle returns null.

If expr and search contain character data, then Oracle compares them using nonpadded comparison semantics. expr, search, and result can be any of the datatypes CHAR, VARCHAR2, NCHAR, or NVARCHAR2. The string returned is of VARCHAR2 datatype and is in the same character set as the first result parameter.

The search, result, and default values can be derived from expressions. Oracle evaluates each search value only before comparing it to expr, rather than evaluating all search values before comparing any of them with expr. Consequently, Oracle never evaluates a search if a previous search is equal to expr.

Oracle automatically converts expr and each search value to the datatype of the first search value before comparing. Oracle automatically converts the return value to the same datatype as the first result. If the first result has the datatype CHAR or if the first result is null, then Oracle converts the return value to the datatype VARCHAR2.

In a DECODE function, Oracle considers two nulls to be equivalent. If expr is null, then Oracle returns the result of the first search that is also null.

The maximum number of components in the DECODE function, including expr, searches, results, and default, is 255.

6-52 Oracle9i SQL Reference

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