SQL基础之时区函数(二十一)

使用数据类型来存储两个日期时间值之间的时间差

使用下列的日期时间函数:

– CURRENT_DATE

– CURRENT_TIMESTAMP

– LOCALTIMESTAMP

– DBTIMEZONE

– SESSIONTIMEZONE

– EXTRACT

– TZ_OFFSET

– FROM_TZ

– TO_TIMESTAMP

– TO_YMINTERVAL

– TO_DSINTERVAL


TIME_ZONE 可以设置为:

绝对偏移量

数据库的时区

OS本地时区

区域名

alter session set time_zone='-05:00';

alter session set time_zone=dbtimezone;

alter session set time_zone=local;

alter session set time_zone='America/New_York';


数据类型范围TIMESTAMP年,月,日,时,分,秒与秒的小数部分TIMESTAMP WITH TIME ZONE与TIMESTAMP数据类型相同;还包括:TIMEZONE_HOUR,TIMEZONE_MINUTE或TIMEZONE_REGION

TIMESTAMP WITH LOCAL TIME ZONE

存储类型与 TIMESTAMP 相似,在用户提交时间给数据库的时,该类型会转换成数据库的时区来保存数据,即数据库保存的时间是数据库本地时区,当用户访问数据库时 oracle 会自动将该时间转换成当前客户端的时间


Datetime  字段有效值YEAR–4712 to 9999 (不包括0年)MONTH01 to 12DAY01 to 31HOUR00 to 23MINUTE00 to 59SECOND00 to 59.9(N) -- 注:9(N)为精度TIMEZONE_HOUR-12 to 14 TIMEZONE_MINUTE00 to 59



create table web_orders (order_date timestamp with time zone,delivery_time timestamp with local time zone);


insert into web_orders values (current_date, current_timestamp + 2);


select * from web_orders;


DATE 与 TIMESTAMP的区别

select hire_date from employees;

SQL 基础之时区函数(二十一)


alter table employees modify hire_date timestamp;

select hire_date from employees;

SQL 基础之时区函数(二十一)


  • CURRENT_DATE:

– 从用户会话返回当前的日期

– 返回的是DATE数据类型

  • CURRENT_TIMESTAMP:

– 从用户会话返回当前的日期和时间

– 返回的是TIMESTAMP WITH TIME ZONE数据类型

  • LOCALTIMESTAMP:

– 从用户会话返回当前的日期和时间

– 返回的是TIMESTAMP数据类型


将参数TIME_ZONE设置为–5:00,然后使用SELECT 语句查看每个日期和时间的差异比较。

alter session set nls_date_format='DD-MON-YYYY HH24:MI:SS';

alter session set time_zone='-5:00';

select sessiontimezone, current_date from dual;

SESSIONTIMEZONE   当前日期

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

,凌晨5时,,,,,,,,,,,,-2017年,27-3 01:12:37


选择sessiontimezone,从双current_timestamp;

sessiontimezone,CURRENT_TIMESTAMP

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

,凌晨5时,,,,,,,,,,,,-17年,27-3 01.13.23.473132,凌晨


选择sessiontimezone,从双localtimestamp;

sessiontimezone,LOCALTIMESTAMP

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

,凌晨5时,,,,,,,,,,,,null

SQL基础之时区函数(二十一)