TIMESTAMP和TIMESTAMP WITH TIME ZONE之間的總結
TIMEZONE指的是當地時間與本初子午線英格蘭格林威治時間的時差
北京是東八區(+08:00),即北京時間-格林威治=8小時,北京比格林威治早8小時看到太陽
SELECT DBTIMEZONE,SESSIONTIMEZONE,LOCALTIMESTAMP,CURRENT_TIMESTAMP,SYSTIMESTAMP FROM DUAL;
+00:00 +08:00 2018/4/17 20:05:26.192987 2018/4/17 20:05:26.192987 +08:00 2018/4/17 20:05:26.192983 +08:00
alter session set time_zone='+09:00';
SELECT DBTIMEZONE,SESSIONTIMEZONE,LOCALTIMESTAMP,CURRENT_TIMESTAMP,SYSTIMESTAMP FROM DUAL;
+00:00 +09:00 2018/4/17 21:05:02.937731 2018/4/17 21:05:02.937731 +09:00 2018/4/17 20:05:02.937726 +08:00
DBTIMEZONE:返回資料庫時區.
DBTIMEZONE returns the value of the database time zone
SESSIONTIMEZONE:返回當前會話時區
SESSIONTIMEZONE returns the time zone of the current session
LOCALTIMESTAMP:返回session端不帶時區的timestamp格式的當前時間
LOCALTIMESTAMP returns the current date and time in the session time zone in a value of data type TIMESTAMP
CURRENT_TIMESTAMP:返回session端帶時區的timestamp格式的當前時間
CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of data type TIMESTAMP WITH TIME ZONE.
SYSTIMESTAMP:返回帶時區的timestamp格式的當前資料庫時間
SYSTIMESTAMP returns the system date, including fractional seconds and time zone, of the system on which the database resides. The return type is TIMESTAMP WITH TIME ZONE.
alter session set time_zone='+08:00';
CREATE TABLE test1 (ID number(2),t_timezone timestamp with time zone,t_local_zone timestamp with local time zone);
insert into test1 values (2,systimestamp,systimestamp);
select * from test1;
2 2018/4/17 20:09:03.298221 +08:00 2018/4/17 20:09:03.298221
alter session set time_zone='+09:00';
select * from test1;
2 2018/4/17 20:09:03.298221 +08:00 2018/4/17 21:09:03.298221
總結:利用timestamp時間欄位屬性TIMESTAMP WITH TIME ZONE,可以把資料庫伺服器所在的時間轉化為當前時區的時間,比如當倫敦時間為20180101 02:00:00,則在北京可以看到該時間為20180101 09:00:00。
TZ_OFFSET returns the time zone offset corresponding to the argument based on the date the statement is executed.
TZ_OFFSET根據輸入的引數值,返回時區與0時區相差的小時和分鐘數。
SELECT TZ_OFFSET('Asia/Shanghai'),TZ_OFFSET('US/Michigan'),TZ_OFFSET('Europe/London') FROM DUAL;
+08:00 -04:00 +01:00
select TZNAME from V$TIMEZONE_NAMES--查詢所有的time_zone_name
FROM_TZ converts a timestamp value and a time zone to a TIMESTAMP WITH TIME ZONE value.
FROM_TZ將時間戳值和時區轉換為具有時區值的時間戳。
SELECT FROM_TZ(TIMESTAMP'2018-04-30 08:00:00','Asia/Shanghai'),FROM_TZ(TIMESTAMP'2018-04-30 08:00:00','US/Michigan'),FROM_TZ(TIMESTAMP'2018-04-30 08:00:00','Europe/London') FROM DUAL;
2018/4/30 8:00:00.000000000 +08:00 2018/4/30 8:00:00.000000000 -04:00 2018/4/30 8:00:00.000000000 +01:00
SELECT FROM_TZ(LOCALTIMESTAMP,'Asia/Shanghai'),FROM_TZ(LOCALTIMESTAMP,'US/Michigan'),FROM_TZ(LOCALTIMESTAMP,'Europe/London') FROM DUAL;
2018/4/17 20:14:47.519347 +08:00 2018/4/17 20:14:47.519347 -04:00 2018/4/17 20:14:47.519347 +01:00
SELECT FROM_TZ(SYSTIMESTAMP,'Asia/Shanghai'),FROM_TZ(SYSTIMESTAMP,'US/Michigan'),FROM_TZ(SYSTIMESTAMP,'Europe/London') FROM DUAL;--因為SYSTIMESTAMP本身帶有時區,所以報錯ORA-00932: 資料型別不一致: 應為 TIMESTAMP, 但卻獲得 TIMESTAMP WITH TIME ZONE。
北京是東八區(+08:00),即北京時間-格林威治=8小時,北京比格林威治早8小時看到太陽
SELECT DBTIMEZONE,SESSIONTIMEZONE,LOCALTIMESTAMP,CURRENT_TIMESTAMP,SYSTIMESTAMP FROM DUAL;
+00:00 +08:00 2018/4/17 20:05:26.192987 2018/4/17 20:05:26.192987 +08:00 2018/4/17 20:05:26.192983 +08:00
alter session set time_zone='+09:00';
SELECT DBTIMEZONE,SESSIONTIMEZONE,LOCALTIMESTAMP,CURRENT_TIMESTAMP,SYSTIMESTAMP FROM DUAL;
+00:00 +09:00 2018/4/17 21:05:02.937731 2018/4/17 21:05:02.937731 +09:00 2018/4/17 20:05:02.937726 +08:00
DBTIMEZONE:返回資料庫時區.
DBTIMEZONE returns the value of the database time zone
SESSIONTIMEZONE:返回當前會話時區
SESSIONTIMEZONE returns the time zone of the current session
LOCALTIMESTAMP:返回session端不帶時區的timestamp格式的當前時間
LOCALTIMESTAMP returns the current date and time in the session time zone in a value of data type TIMESTAMP
CURRENT_TIMESTAMP:返回session端帶時區的timestamp格式的當前時間
CURRENT_TIMESTAMP returns the current date and time in the session time zone, in a value of data type TIMESTAMP WITH TIME ZONE.
SYSTIMESTAMP:返回帶時區的timestamp格式的當前資料庫時間
SYSTIMESTAMP returns the system date, including fractional seconds and time zone, of the system on which the database resides. The return type is TIMESTAMP WITH TIME ZONE.
alter session set time_zone='+08:00';
CREATE TABLE test1 (ID number(2),t_timezone timestamp with time zone,t_local_zone timestamp with local time zone);
insert into test1 values (2,systimestamp,systimestamp);
select * from test1;
2 2018/4/17 20:09:03.298221 +08:00 2018/4/17 20:09:03.298221
alter session set time_zone='+09:00';
select * from test1;
2 2018/4/17 20:09:03.298221 +08:00 2018/4/17 21:09:03.298221
總結:利用timestamp時間欄位屬性TIMESTAMP WITH TIME ZONE,可以把資料庫伺服器所在的時間轉化為當前時區的時間,比如當倫敦時間為20180101 02:00:00,則在北京可以看到該時間為20180101 09:00:00。
TZ_OFFSET returns the time zone offset corresponding to the argument based on the date the statement is executed.
TZ_OFFSET根據輸入的引數值,返回時區與0時區相差的小時和分鐘數。
SELECT TZ_OFFSET('Asia/Shanghai'),TZ_OFFSET('US/Michigan'),TZ_OFFSET('Europe/London') FROM DUAL;
+08:00 -04:00 +01:00
select TZNAME from V$TIMEZONE_NAMES--查詢所有的time_zone_name
FROM_TZ converts a timestamp value and a time zone to a TIMESTAMP WITH TIME ZONE value.
FROM_TZ將時間戳值和時區轉換為具有時區值的時間戳。
SELECT FROM_TZ(TIMESTAMP'2018-04-30 08:00:00','Asia/Shanghai'),FROM_TZ(TIMESTAMP'2018-04-30 08:00:00','US/Michigan'),FROM_TZ(TIMESTAMP'2018-04-30 08:00:00','Europe/London') FROM DUAL;
2018/4/30 8:00:00.000000000 +08:00 2018/4/30 8:00:00.000000000 -04:00 2018/4/30 8:00:00.000000000 +01:00
SELECT FROM_TZ(LOCALTIMESTAMP,'Asia/Shanghai'),FROM_TZ(LOCALTIMESTAMP,'US/Michigan'),FROM_TZ(LOCALTIMESTAMP,'Europe/London') FROM DUAL;
2018/4/17 20:14:47.519347 +08:00 2018/4/17 20:14:47.519347 -04:00 2018/4/17 20:14:47.519347 +01:00
SELECT FROM_TZ(SYSTIMESTAMP,'Asia/Shanghai'),FROM_TZ(SYSTIMESTAMP,'US/Michigan'),FROM_TZ(SYSTIMESTAMP,'Europe/London') FROM DUAL;--因為SYSTIMESTAMP本身帶有時區,所以報錯ORA-00932: 資料型別不一致: 應為 TIMESTAMP, 但卻獲得 TIMESTAMP WITH TIME ZONE。
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30126024/viewspace-2153032/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- timestamp with local time zone型別和timestamp with time zone型別
- 6、TIMESTAMP WITH TIME ZONE和TIMESTAMP WITH LOCAL TIME ZONE的實踐理解
- oracle之time zone時區timestamp with time zone相關的概念之一Oracle
- date和timestamp型別之間的轉換型別
- String和Date、Timestamp之間的轉換
- oracle plsql定義date_timestamp_with time zoneOracleSQL
- MySQL案例之Timestamp和DatetimeMySql
- MySQL之timestampMySql
- mysql CURRENT_TIMESTAMP 和ON UPDATE CURRENT_TIMESTAMPMySql
- Java:String和Date、Timestamp之間的轉換Java
- Oracle timestamp_to_scn and scn_to_timestampOracle
- Oracle TimestampOracle
- [Warning] TIMESTAMP with implicit DEFAULT --explicit_defaults_for_timestamp
- ORA-39322: Cannot use transportable tablespace with timestamp with timezone...
- Timestamp-時間戳轉換時間戳
- SQL Server中timestamp(時間戳)SQLServer時間戳
- Mysql中的Datetime和Timestamp比較MySql
- partitioned by timestamp datatype
- MySQL 5.6 timestamp和datetime區別MySql
- [Warning] TIMESTAMP with implicit DEFAULT value is deprecated.explicit_defaults_for_timestamp
- 11g AUDIT的TIMESTAMP時間戳時間戳
- MySQL: datetime vs timestampMySql
- MySQL 的 timestamp 和 datetime 型別比較MySql型別
- Sybase的timestamp型別型別
- Oracle中Date和Timestamp的區別Oracle
- JavaScript event.timeStampJavaScript
- jQuery event.timeStampjQuery
- 深入淺出timestamp
- java convert timestamp to date and timeJava
- Elasticsearch為記錄新增時間戳timestampElasticsearch時間戳
- MySQL中的CURRENT_TIMESTAMPMySql
- oracle的timestamp型別使用Oracle型別
- TIMESTAMP的時區轉換
- TIMESTAMP型別的時區型別
- Oracle中的TIMESTAMP型別Oracle型別
- oracle timestamp的最大值Oracle
- How to get SCN ,TIMESTAMP ,CHECKPOINT
- 輸入TIMESTAMP型別型別