(Les16 Managing Data in Different Time Zones)[20171230]
阿新 • • 發佈:2017-12-31
num 調整 char 所有 comm mit 有時 小數 ber
學習目標:
-current_date 當前時間 TZ_OFFSET
-current_timestamp 含小數秒當前時間 FROM_TZ
-localtimestamp 含小數秒本地時間 TO_TIMESTAMP
-dbtimezone 數據庫時區 TO_TIMSTAMP_TZ
-sessiontimezone 回話時間時區 TO_YMINTERVAL
-extract 提取 TO_DSINTERVAL
TIME_ZONE會話參數
可以設定為以下值
-一個絕對偏移值
-數據庫時區
-系統本地時區
-一個區域名稱
ALTER SESSION SET TIME_ZONE = ‘-06:00‘;
ALTER SESSION SET TIME_ZONE = dbtimezone;
ALTER SESSION SET TIME_ZONE = local;
ALTER SESSION SET TIME_ZONE = ‘America/New_York‘;
INTERVAL時間類型 -INTERVAL用於存儲兩個時間差 -年-月 Year-month INTERVAL YEAR TO MONTH -天-時間 Day-time INTERVAL DAY TO SECOND INTERVAL YEAR (year_precision) TO MONTH INTERVAL DAY (day_precision) TO SECOND (fractional_seconds_precision)
學習總結: 1.5個時間current_date、current_timestamp、localtimestamp、dbtimezone、sessionzone 2.時區值的更改及對應可選值數據庫時區、系統時區、偏移量值、時區區域名稱 3.時間類型:DATE ,TIMESTAMP,TIMESTAMP WITH TIME ZONE ,TIMESTAMP WITH LOCAL TIME ZONE,INTERVAL YEAR TO MONTH,INTERVAL DAY TO SECOND 4.時間類型中的各個值的提取函數EXTRACT 5.時間類型的轉換函數TO_DATE,TZ_OFFSET,FROM_TZ,TO_TIMESTAMP,TO_TIMSTAMP_TZ,TO_YMINTERVAL,TO_DSINTERVAL
SYS@cqoraxt0CURRENT_DATE,CURRENT_TIMESTAMP,LOCALTIMESTAMP -CURRENT_DATE 當前時間同sysdate表達一樣 -CURRENT_TIMESTAMP 含時區 -LOCALTIMESTAMP 不含時區>ALTER SESSION SET TIME_ZONE = dbtimezone; Session altered. SYS@cqoraxt0>select systimestamp from dual; SYSTIMESTAMP --------------------------------------------------------------------------- 29-DEC-17 10.50.13.546588 PM +08:00 ->當前數據庫所在時區的時間 SYS@cqoraxt0>select localtimestamp from dual; LOCALTIMESTAMP--------------------------------------------------------------------------- 29-DEC-17 02.50.38.464141 PM SYS@cqoraxt0>ALTER SESSION SET TIME_ZONE = dbtimezone; Session altered. SYS@cqoraxt0>select localtimestamp from dual; LOCALTIMESTAMP ---------------------------------------------------------------------------29-DEC-17 02.50.49.337510 PM SYS@cqoraxt0>ALTER SESSION SET TIME_ZONE = ‘-06:00‘; Session altered. SYS@cqoraxt0>select localtimestamp from dual; LOCALTIMESTAMP --------------------------------------------------------------------------- 29-DEC-17 08.51.00.307601 AM->絕對偏移量的時區 SYS@cqoraxt0>ALTER SESSION SET TIME_ZONE = local; Session altered. SYS@cqoraxt0>select localtimestamp from dual; LOCALTIMESTAMP --------------------------------------------------------------------------- 29-DEC-17 10.51.11.989536 PM->系統本地時間時區 SYS@cqoraxt0>ALTER SESSION SET TIME_ZONE = ‘America/New_York‘; Session altered. SYS@cqoraxt0>select localtimestamp from dual; LOCALTIMESTAMP --------------------------------------------------------------------------- 29-DEC-17 09.51.21.599491 AM ->指定時間區域名稱
col CURRENT_DATE format a30 col SESSIONTIMEZONE format a20 col CURRENT_TIMESTAMP format a40 col LOCALTIMESTAMP format a40 col sysdate format a30 set linesize 300 alter session set nls_date_format=‘YYYY-MM-DD HH24:MI:SS‘; --調整時間輸出的格式 select sessiontimezone,current_date,current_timestamp,localtimestamp,sysdate from dual; SESSIONTIMEZONE CURRENT_DATE CURRENT_TIMESTAMP LOCALTIMESTAMP SYSDATE -------------------- ------------------------------ ---------------------------------------- ---------------------------------------- ------------------------------ +08:00 2017-12-29 23:13:08 29-DEC-17 11.13.08.826336 PM +08:00 29-DEC-17 11.13.08.826336 PM 2017-12-29 23:13:08
DBTIMEZONE,SESSIONTIMEZONE -DBTIMEZONE 數據庫時區,也就是0時區 -SESSIONTIMEZONE 會話的當前時區
col SESSIONTIMEZONE format a20 col sysdate format a30 set linesize 300 alter session set nls_date_format=‘YYYY-MM-DD HH24:MI:SS‘; --調整時間輸出的格式 select dbtimezone,sessiontimezone,sysdate from dual; DBTIME SESSIONTIMEZONE SYSDATE ------ -------------------- ------------------------------ +00:00 +08:00 2017-12-29 23:16:04
TIMESTAMP 時間類型 -TIMESTAMP [(fractional_seconds_precision)] 包含:年,月,天,小時,分鐘,秒,時區小時,時區分鐘 -TIMESTAMP [(fractional_seconds_precision)] WITH TIME ZONE -TIMESTAMP [(fractional_seconds_precision)] WITH LOCAL TIME ZONE TIMESTAMP時間類型各個值的範圍 DATE,TIMESTAMP的不同點 -TIMESTAMP相比DATE多小數秒,時區小時和時區分鐘。
create table time_t01( id number, t_date date default sysdate, t_timestamp timestamp default current_timestamp ) / insert into time_t01 values(1,default,default); / col t_date format a30 col t_timestamp format a40 set linesize 300 alter session set nls_date_format=‘YYYY-MM-DD HH24:MI:SS‘; --調整會話輸出時時間格式 select * from time_t01; ID T_DATE T_TIMESTAMP ---------- ------------------------------ ---------------------------------------- 1 2017-12-29 23:51:55 29-DEC-17 11.51.55.085972 PM
--添加一個欄位
alter table time_t01 add (t_timestamp_zone timestamp with time zone); insert into time_t01 values(2,default,default,timestamp‘2017-12-29 23:57:00 +8:00‘); commit; insert into time_t01 values(3,default,default,timestamp‘2017-12-29 23:57:00 America/New_York‘); commit; insert into time_t01 values(4,default,default,timestamp‘2017-12-29 23:57:00 -8:00‘); commit; col t_timestamp_zone format a50 select * from time_t01; ID T_DATE T_TIMESTAMP T_TIMESTAMP_ZONE ---------- ------------------------------ ---------------------------------------- -------------------------------------------------- 1 2017-12-29 23:51:55 29-DEC-17 11.51.55.085972 PM 2 2017-12-29 23:57:39 29-DEC-17 11.57.39.696414 PM 29-DEC-17 11.57.00.000000 PM +08:00 3 2017-12-30 00:02:51 30-DEC-17 12.02.51.376596 AM 29-DEC-17 11.57.00.000000 PM AMERICA/NEW_YORK 4 2017-12-30 00:02:51 30-DEC-17 12.02.51.385169 AM 29-DEC-17 11.57.00.000000 PM -08:00 --添加一個欄位 alter table time_t01 add (t_timestamp_local_zone timestamp with local time zone); insert into time_t01 values(5,default,default,current_timestamp,current_timestamp); col t_timestamp_local_zone format a50 select * from time_t01 order by id; ID T_DATE T_TIMESTAMP T_TIMESTAMP_ZONE T_TIMESTAMP_LOCAL_ZONE ---------- ------------------------------ ---------------------------------------- -------------------------------------------------- -------------------------------------------------- 1 2017-12-29 23:51:55 29-DEC-17 11.51.55.085972 PM 2 2017-12-29 23:57:39 29-DEC-17 11.57.39.696414 PM 29-DEC-17 11.57.00.000000 PM +08:00 3 2017-12-30 00:02:51 30-DEC-17 12.02.51.376596 AM 29-DEC-17 11.57.00.000000 PM AMERICA/NEW_YORK 4 2017-12-30 00:02:51 30-DEC-17 12.02.51.385169 AM 29-DEC-17 11.57.00.000000 PM -08:00 5 2017-12-30 00:07:05 30-DEC-17 12.07.05.688410 AM 30-DEC-17 12.07.05.688410 AM +08:00 30-DEC-17 12.07.05.688410 AM 改變會話當前時區,timestamp with local time zone 時間也會跟著變動 SYS@cqoraxt0>alter session set time_zone=‘-09:00‘; Session altered. SYS@cqoraxt0> select * from time_t01 order by id; ID T_DATE T_TIMESTAMP T_TIMESTAMP_ZONE T_TIMESTAMP_LOCAL_ZONE ---------- ------------------------------ ---------------------------------------- -------------------------------------------------- -------------------------------------------------- 1 2017-12-29 23:51:55 29-DEC-17 11.51.55.085972 PM 2 2017-12-29 23:57:39 29-DEC-17 11.57.39.696414 PM 29-DEC-17 11.57.00.000000 PM +08:00 3 2017-12-30 00:02:51 30-DEC-17 12.02.51.376596 AM 29-DEC-17 11.57.00.000000 PM AMERICA/NEW_YORK 4 2017-12-30 00:02:51 30-DEC-17 12.02.51.385169 AM 29-DEC-17 11.57.00.000000 PM -08:00 5 2017-12-30 00:07:05 30-DEC-17 12.07.05.688410 AM 30-DEC-17 12.07.05.688410 AM +08:00 29-DEC-17 07.07.05.688410 AM
INTERVAL時間類型 -INTERVAL用於存儲兩個時間差 -年-月 Year-month INTERVAL YEAR TO MONTH -天-時間 Day-time INTERVAL DAY TO SECOND INTERVAL YEAR (year_precision) TO MONTH INTERVAL DAY (day_precision) TO SECOND (fractional_seconds_precision)
create table time_t02( id number, t_interval_y interval year to month, t_interval_d interval day to second ) / insert into time_t02 values(1,‘03-5‘ ,‘05 12:00:00‘) / insert into time_t02 values(2,interval ‘04‘ year,interval ‘06 13:00:00‘ day to second ) / insert into time_t02 values(3,interval ‘12‘ month,interval ‘07 14:00:00‘ day to second ) / alter session set nls_date_format=‘YYYY-MM-DD HH24:MI:SS‘; col t_interval_y format a30 col t_interval_d format a30 select * from time_t02; ID T_INTERVAL_Y T_INTERVAL_D ---------- ------------------------------ ------------------------------ 1 +03-05 +05 12:00:00.000000 2 +04-00 +06 13:00:00.000000 3 +01-00 +06 13:00:00.000000EXTRACT 提取時間 -可提取時間類型 EXTRACT ([YEAR] [MONTH][DAY] [HOUR] [MINUTE][SECOND] [TIMEZONE_HOUR] [TIMEZONE_MINUTE] [TIMEZONE_REGION] [TIMEZONE_ABBR] -年、月、天、小時、分鐘、秒、時區小時、時區分鐘、時區區域、時區縮寫
set linezie 600 col zone_region format a20 col zone_abbr format a20 select extract(year from t_timestamp_zone) year, extract(month from t_timestamp_zone) month, extract(day from t_timestamp_zone) day, extract(hour from t_timestamp_zone) hour, extract(minute from t_timestamp_zone) minute, extract(second from t_timestamp_zone) second, extract(timezone_hour from t_timestamp_zone) zone_hour, extract(timezone_minute from t_timestamp_zone) zone_minute, extract(timezone_region from t_timestamp_zone) zone_region, extract(timezone_abbr from t_timestamp_zone) zone_abbr from time_t01 where id=3 / YEAR MONTH DAY HOUR MINUTE SECOND ZONE_HOUR ZONE_MINUTE ---------- ---------- ---------- ---------- ---------- ---------- ---------- ----------- ZONE_REGION ZONE_ABBR -------------------- -------------------- 2017 12 30 4 57 0 -5 0 America/New_York EST時區轉換函數 -TZ_OFFSET 時間區域轉為為數值 TZ_OFFSET ( [‘time_zone_name‘] ‘[+ | -] hh:mm‘ ] [ SESSIONTIMEZONE] [DBTIMEZONE]
SYS@cqoraxt0>select tz_offset(‘America/New_York‘) from dual; TZ_OFFS ------- -05:00V$TIMEZONE_NAMES所有時區區域名稱視圖 -FROM_TZ 將TIMESTAMP轉換為TIMESTAMP WITH TIME ZONE FROM_TZ(TIMESTAMP timestamp_value, time_zone_value)
SYS@cqoraxt0>select from_tz(timestamp ‘2017-12-30 23:50:00‘,‘+8:00‘) from dual; FROM_TZ(TIMESTAMP‘2017-12-3023:50:00‘,‘+8:00‘) --------------------------------------------------------------------------- 30-DEC-17 11.50.00.000000000 PM +08:00-TO_TIMESTAMP 將字符串轉為TIMESTAMP時間類型 TO_TIMESTAMP (char,[fmt],[‘nlsparam‘])
SYS@cqoraxt0>select to_timestamp(‘2017-12-30 23:53:00‘,‘YYYY-MM-DD HH24:MI:SS‘) from dual; TO_TIMESTAMP(‘2017-12-3023:53:00‘,‘YYYY-MM-DDHH24:MI:SS‘) --------------------------------------------------------------------------- 30-DEC-17 11.53.00.000000000 PM
-TO_TIMESTAMP_TZ 將字符串轉為TIMESTAMP WITH TIME ZONE TO_TIMESTAMP_TZ (char,[fmt],[‘nlsparam‘])
SYS@cqoraxt0>select to_timestamp_tz(‘2017-12-30 23:55:00 +8:00‘,‘YYYY-MM-DD HH24:MI:SS TZH:TZM‘) from dual; TO_TIMESTAMP_TZ(‘2017-12-3023:55:00+8:00‘,‘YYYY-MM-DDHH24:MI:SSTZH:TZM‘) --------------------------------------------------------------------------- 30-DEC-17 11.55.00.000000000 PM +08:00-TO_YMINTERVAL 將字符串轉為時間間隔類型INTERVAL YEAR TO MONTH TO_YMINTERVAL(char)
SYS@cqoraxt0>select sysdate+to_yminterval(‘01-01‘) from dual; SYSDATE+TO_YMINTERV ------------------- 2019-01-31 00:03:14
-TO_DSINTERVAL 將字符串轉為時間間隔類型INTERVAL DAY TO SECOND
TO_DSINTERVAL(char)SYS@cqoraxt0>select sysdate+to_dsinterval(‘05 1:00:00‘) from dual; SYSDATE+TO_DSINTERV ------------------- 2018-01-05 01:06:57
學習總結: 1.5個時間current_date、current_timestamp、localtimestamp、dbtimezone、sessionzone 2.時區值的更改及對應可選值數據庫時區、系統時區、偏移量值、時區區域名稱 3.時間類型:DATE ,TIMESTAMP,TIMESTAMP WITH TIME ZONE ,TIMESTAMP WITH LOCAL TIME ZONE,INTERVAL YEAR TO MONTH,INTERVAL DAY TO SECOND 4.時間類型中的各個值的提取函數EXTRACT 5.時間類型的轉換函數TO_DATE,TZ_OFFSET,FROM_TZ,TO_TIMESTAMP,TO_TIMSTAMP_TZ,TO_YMINTERVAL,TO_DSINTERVAL
(Les16 Managing Data in Different Time Zones)[20171230]