Oracle Globalization - time type, time zone and time correlation function

When recently encountered in a data table area right questions, troubleshoot general idea is to look at the table field type, time zone, and ways of looking at the insertion time to see. Oracle official documentation "Database Globalization Support Guide" There is a very detailed description, inductive learning about.

 

First, the time type

Oracle in time type divided into two categories - Datetime and the Data Types Interval , this article focuses on the first category Datetime.

Datetime can be divided into four categories , which are associated with the time zone after two categories:

  • DATE
  • TIMESTAMP
  • TIMESTAMP WITH TIME ZONE
  • TIMESTAMP WITH LOCAL TIME ZONE

 

1. DATE

Storage date + time, accurate to the second, and the time zone area information is not stored. Determined by the output format and language NLS_DATE_LANGUAGE NLS_DATE_FORMAT and two initialization parameters. If you do not specify a query when these two parameters is not typecast, will by default output format.

SQL> select sysdate from dual;

SYSDATE
-------------------
2014-02-12 01:12:18

 

2. TIMESTAMP

DATE type of expansion, the storage date + time, accurate to the second decimal 0 to 9 (default 6), and the time zone is not stored area information. By the output format and language NLS_TIMESTAMP_FORMAT and NLS_DATE_LANGUAGE  decision two initialization parameters. If you do not specify a query when these two parameters is not typecast, will by default output format.

SQL> select localtimestamp from dual;

LOCALTIMESTAMP
---------------------------------------------------------------------------
12-FEB-14 01.14.12.945256 AM

SQL> alter session set nls_timestamp_format='YYYY-MM-DD HH24:MI:SSXFF';
Session altered.

SQL> select localtimestamp from dual;

LOCALTIMESTAMP
---------------------------------------------------------------------------
2014-02-12 01:28:31.652888

 

3. TIMESTAMP WITH TIME ZONE

TIMESTAMP type of expansion, the storage date + time, accurate to the second decimal 0 to 9 (default 6), when the memory area (or the time zone and area) information . This type of data with the current client session timezone when saved to the database, no matter what time zone view this data, the data will not change any time zone.

create table t1 (id number,time timestamp with time zone);  --创建t1表,其中time 列的数据类型是timestamp with time zone
Table created.

select sessiontimezone from dual; --当前客户端的session timezone 是 -8:00
SESSIONTIMEZONE
---------------------------------------------------------------------------
-08:00

insert into t1 values(1,timestamp '2014-02-12 02:00:00');  --向t1表中插入一条数据  
1 row created.

select * from t1;  --查看t1表,其中time列带时区显示,并且时区为数据被插入时的session timezone
        ID     TIME
----------   ---------------------------------------------------------------------------
         1     2014-02-12 02:00:00.000000 -08:00

alter session set time_zone='-6:00';  --修改当前客户端的session timezone为 -6:00
Session altered.

select * from t1;    --再次查看t1表,其中time列数据无变化
        ID     TIME
----------   ---------------------------------------------------------------------------
         1     2014-02-12 02:00:00.000000 -08:00

 

4. TIMESTAMP WITH LOCAL TIME ZONE

Another type TIMESTAMP extended, storage date + time, accurate to the second decimal 0 to 9 (default 6), the time zone information is not stored, but the time the client after input into the database based conversion database timezone (this is the meaning of database tmiezone setting where, as a TIMESTAMP WITH LOCAL TIME ZONE type of computing scale). When a user queries this type of data, Oracle will then converted to data of the user session time zone back to the user.

A time zone Client -> time zone database database tmiezone set - time zone> Client B

create table t2(id number,time timestamp with local time zone);  -- 创建t2表,其中time列为TIMESTAMP WITH LOCAL TIME ZONE
Table created.

insert into t2 values(1,timestamp '2014-02-12 02:10:00 -8:00');  --在t2表插入数据指定时区为-8:00,实际在保存到数据库时转化为基于database timezone的时间保存    
1 row created. 

select sessiontimezone from dual;  --当前客户端的session timezone 为 -6:00
SESSIONTIMEZONE
---------------------------------------------------------------------------
-06:00

select * from t2;  --查看时oracle将数据转换成当前客户端session timezone的时间
        ID      TIME
----------   ---------------------------------------------------------------------------
         1       2014-02-12 04:10:00.000000

 

The choice of the type of time

  • DATE: Time required accuracy is not high, do not need to save the time zone / Area Information
  • TIMESTAMP: high temporal precision required, no need to save the time zone / Area Information
  • TIMESTAMP WITH TIME ZONE: Time zone / region information needs to be saved. Such as the need accurate records of the time and place of each transaction (time zone) to see if it happened in a few local
  • TIMESTAMP WITH LOCAL TIME ZONE: do not care about location operation takes place, is only interested in operating in your current location when the occurrence of a few areas. For example, there is a TV broadcasting in Japan time ten o'clock, but in fact, I only care about a few Chinese can catch live together in time, for me, the most convenient is a search database directly told me it began broadcasting in China nine times.

 

Second, the time zone

In fact, according to an earlier already know, distinguish between the two when Oracle - time zone and time zone database session

1. Database time zone

As TIMESTAMP WITH LOCAL TIME ZONE type of computing scales.

Query methods

SELECT dbtimezone FROM DUAL;

Setting method

  • You can specify a SET TIME_ZONE clause in the CREATE DATABASE.
CREATE DATABASE db01
...
SET TIME_ZONE='Europe/London';
-- 或者
CREATE DATABASE db01
...
SET TIME_ZONE='-05:00';
  • The latter can also be modified (DB restart to take effect)
ALTER DATABASE SET TIME_ZONE='Europe/London';
--或者
ALTER DATABASE SET TIME_ZONE='-05:00';

 

2. The session time zone

Sql when the current session's time zone, the default is the server operating system's time zone.

Query methods

SELECT sessiontimezone FROM DUAL;

Setting method

  • You can set the operating system environment variables ORA_SDTZ
setenv ORA_SDTZ 'OS_TZ'  #默认
setenv ORA_SDTZ 'DB_TZ'
setenv ORA_SDTZ 'Europe/London'
setenv ORA_SDTZ '-05:00'
  • You can also use the sql command set
ALTER SESSION SET TIME_ZONE=local; -- 相当于os
ALTER SESSION SET TIME_ZONE=dbtimezone;
ALTER SESSION SET TIME_ZONE='Asia/Hong_Kong';
ALTER SESSION SET TIME_ZONE='+10:00';

 

Third, the time correlation function

Datetime function operable date (DATE), timestamp (TIMESTAMP, TIMESTAMP WITH TIME ZONE, TIMESTAMP WITH LOCAL TIME ZONE) and interval (INTERVAL DAY TO SECOND, INTERVAL YEAR TO MONTH) value types.

 

1. Datetime Functions Designed for the DATE Data Type

Function Description

ADD_MONTHS

Returnns the date d plus n months

LAST_DAY

Returns the last day of the month that contains date

MONTHS_BETWEEN

Returns the number of months between date1 and date2

NEW_TIME

Returns the date and time in zone2 time zone when the date and time in zone1 time zone are date

Note: This function takes as input only a limited number of time zones. You can have access to a much greater number of time zones by combining the FROM_TZ function and the datetime expression.

NEXT_DAY

Returnns the date of the first weekday named by char that is later than date

ROUND(date)

Returnns date rounded to the unit specified by the fmt format model

TRUNC(date)

Returnns date with the time portion of the day truncated to the unit specified by the fmt format model

 

2. Additional Datetime Functions

Datetime Function Description

CURRENT_DATE

Returns the current date in the session time zone in a value in the Gregorian calendar, of the DATE data type

CURRENT_TIMESTAMP

Returns the current date and time in the session time zone as a TIMESTAMP WITH TIME ZONE value

DBTIMEZONE

Returns the value of the database time zone. The value is a time zone offset or a time zone region name

EXTRACT (datetime)

Extracts and returns the value of a specified datetime field from a datetime or interval value expression

FROM_TZ

Converts TIMESTAMP value at a time zone to a TIMESTAMP WITH TIME ZONE value

LOCALTIMESTAMP

Returns the current date and time in the session time zone in a value of the TIMESTAMP data type

NUMTODSINTERVAL

Converts number n to an INTERVAL DAY TO SECOND literal

NUMTOYMINTERVAL

Converts number n to an INTERVAL YEAR TO MONTH literal

SESSIONTIMEZONE

Returnns the value of the current session's time zone

SYS_EXTRACT_UTC

Extracts the UTC from a datetime with time zone offset

SYSDATE

Returns the date and time of the operating system on which the database resides, taking into account the time zone of the database server's operating system that was in effect when the database was started

SYSTIMESTAMP

Returns the system date, including fractional seconds and time zone of the system on which the database resides

TO_CHAR (datetime)

Converts a datetime or interval value of DATETIMESTAMPTIMESTAMP WITH TIME ZONE, or TIMESTAMP WITH LOCAL TIME ZONE data type to a value of VARCHAR2 data type in the format specified by the fmt date format

TO_DSINTERVAL

Converts a character string of CHARVARCHAR2NCHAR, or NVARCHAR2 data type to a value of INTERVAL DAY TO SECOND data type

TO_NCHAR (datetime)

Converts a datetime or interval value of DATETIMESTAMPTIMESTAMP WITH TIME ZONETIMESTAMP WITH LOCAL TIME ZONEINTERVAL MONTH TO YEAR, or INTERVAL DAY TO SECOND data type from the database character set to the national character set

TO_TIMESTAMP

Converts a character string of CHARVARCHAR2NCHAR, or NVARCHAR2 data type to a value of TIMESTAMP data type

TO_TIMESTAMP_TZ

Converts a character string of CHARVARCHAR2NCHAR, or NVARCHAR2 data type to a value of the TIMESTAMP WITH TIME ZONE data type

TO_YMINTERVAL

Converts a character string of CHARVARCHAR2NCHAR, or NVARCHAR2 data type to a value of the INTERVAL YEAR TO MONTH data type

TZ_OFFSET

Returns the time zone offset that corresponds to the entered value, based on the date that the statement is executed

 

3. Time Zone Conversion Functions

Time Zone Function Description

ORA_DST_AFFECTED

Enables you to verify whether the data in a column is affected by upgrading the DST rules from one version to another version

ORA_DST_CONVERT

Enables you to upgrade your TSTZ column data from one version to another

ORA_DST_ERROR

Enables you to verify that there are no errors when upgrading a datetime value

 

参考

https://docs.oracle.com/en/database/oracle/oracle-database/19/nlspg/datetime-data-types-and-time-zone-support.html#GUID-7A1BA319-767A-43CC-A579-4DAC7063B243

http://blog.itpub.net/29457434/viewspace-1080444/

发布了295 篇原创文章 · 获赞 35 · 访问量 8万+

Guess you like

Origin blog.csdn.net/Hehuyi_In/article/details/104883708