2016-01-08
一、时间值(datetime)
alter session set NLS_DATE_FORMAT ='MONTH-DD-YYYY'; alter session set NLS_DATE_FORMAT ='DD-MON-YYYY'; --设置默认时间格式 insert into customers (customer_id,first_name,last_name,dob,phone) values (6,'Fred','Brown','05-Feb-1968','800-555-1215'); insert into customers (customer_id,first_name,last_name,dob,phone) values (7,'Steve','Purple',DATE '1972-10-25','800-555-1215');
1.TO_CHAR(x[,format]) (重点!!!)
--将时间值x转换为字符串 select customer_id,to_char(dob,'MONTH DD,YYYY') from customers; select to_char(SYSDATE,'MONTH DD,YYYY,HH24:MI:SS') from dual; select to_char(to_date('05-FEB-1968'),'MONTH DD, YYYY') from dual;
2.TO_DATE(x[,format]) (重点!!!)
--将字符串转换为时间值 select to_date('04-JUL-2012'),to_date('04-JUL-12') from dual; select to_date('July 4, 2012', 'MONTH DD, YYYY') from dual; select to_date('7.4.12','MM.DD.YY') from dual; insert into customers (customer_id,first_name,last_name,dob,phone) values (6,'Fred','Brown',to_date('05-FEB-1968 19:32:36', 'DD-MON-YYYY HH24:MI:SS'),'800-555-1215'); select customer_id,to_char(dob,'DD-MON-YYYY HH24:MI:SS') from customers;
3.组合使用TO_CHAR()和TO_DATE()函数
select to_char(to_date('05-FEB-1968 19:32:36','DD-MON-YYYY HH24:MI:SS'), 'HH24:MI:SS') from dual; select to_char(to_date('04-JUL-15', 'DD-MON-YY'), 'DD-MON-YYYY') as "15YY", to_char(to_date('04-JUL-55', 'DD-MON-YY'), 'DD-MON-YYYY') as "55YY", to_char(to_date('04-JUL-75', 'DD-MON-YY'), 'DD-MON-YYYY') as "75YY" from dual; select to_char(to_date('04-JUL-15', 'DD-MON-RR'), 'DD-MON-YYYY') as "15RR", to_char(to_date('04-JUL-55', 'DD-MON-RR'), 'DD-MON-YYYY') as "55RR", to_char(to_date('04-JUL-75', 'DD-MON-RR'), 'DD-MON-YYYY') as "75RR" from dual;
4.ADD_MONTHS(x,y)
--返回x加上y个月后的日期 select add_months('01-JAN-2012',13) from dual; select add_months('01-JAN-2012',-13) from dual; select add_months(to_date('01-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS'),2) from dual; select to_char( add_months( to_date('01-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS' ),2 ),'DD-MON-YYYY HH24:MI:SS' ) from dual;
5.LAST_DAY(x)
--返回包含x的月份的最后一天 select last_day('01-JAN-2012') from dual;
6.MONTHS_BETWEEN(x,y)
--返回x和y之间有几个月 select months_between('25-MAY-2012','15-JAN-2012') from dual; select months_between('15-JAN-2012','25-MAY-2012') from dual;
7.NEXT_DAY(x,day)
--返回从x开始,下一个day的日期 select next_day('01-JAN-2012','SATUERDAY') from dual;
8.ROUND(x[,unit])
--对x取整,默认取整为最近的一天 select round(to_date('25-OCT-2012'),'YYYY') from dual; select round(to_date('25-MAY-2012'),'MM') from dual; select to_char( round( to_date('25-MAY-2012 19:45:26','DD-MON-YYYY HH24:MI:SS' ),'HH24' ),'DD-MON-YYYY HH24:MI:SS' ) from dual;
9.SYSDATE
--返回数据库服务器所在操作系统当前日期 select sysdate from dual;
10.TRUNC(x[,unit])
--对x截断,默认截断为当天开始时间 select trunc(to_date('25-MAY-2012'),'YYYY') from dual; select trunc(to_date('25-MAY-2012'),'MM') from dual; select to_char( trunc( to_date( '25-MAY-2012 19:45:26','DD-MON-YYYY HH24:MI:SS' ),'HH24' ),'DD-MON-YYYY HH24:MI:SS' ) from dual;
二、时区(timezone)
+00:00
GMT(Green Mean Time) 格林威治时间(旧称)
UTC(Coordinated Universal Time)协调世界时间(新称)
-08:00
PST(Pacific Standard Time)太平洋标准时间
-05:00
EST(Eastern Standard Time)美国东部标准时间
alter database set time_zone = offset |region --修改数据库时区 alter database set time_zone = '-8:00' alter database set time_zone = 'PST' alter session set time_zone = 'EST'; --修改会话时区 select dbtimezone from dual; --查看数据库时区 select sessiontimezone from dual; --查看会话时区 select current_date from dual; --查看会话时区的当前日期 select tz_offset('PST') from dual; --获取时区的时差 conn system; select tzname,tzabbrev from v$timezone_names where rownum <= 5 order by tzabbrev; --获取时区名 select to_char( new_time( to_date( '25-MAY-2012 19:45','DD-MON-YYYY HH24:MI' ),'PST','EST' ),'DD-MON-YYYY HH24:MI' ) from dual; --将时间值从一个时区转换到另一个时区
三、时间戳(timestamp)
1.时间戳类型
--时间戳可以存储秒的小数位、时区
1.1 timstamp类型
create table purchases_with_timestamp ( product_id integer references products(product_id), customer_id integer references customers(customer_id), made_on timestamp(4));
insert into purchases_with_timestamp (product_id,customer_id,made_on) values (1,1,timestamp '2005-05-13 07:15:31.1234'); select * from purchases_with_timestamp;
1.2 timstamp with time zone类型
create table purchases_timestamp_with_tz ( product_id integer references products(product_id), customer_id integer references customers(customer_id), made_on timestamp(4) with time zone); insert into purchases_timestamp_with_tz (product_id,customer_id,made_on) values (1,1,timestamp '2005-05-13 07:15:31.1234 -07:00'); insert into purchases_timestamp_with_tz (product_id,customer_id,made_on) values (1,2,timestamp '2005-05-13 07:15:31.1234 PST'); select * from purchases_timestamp_with_tz;
1.3 timstamp with local time zone类型
create table purchases_with_local_tz ( product_id integer references products(product_id), customer_id integer references customers(customer_id), made_on timestamp(4) with local time zone); insert into purchases_with_local_tz (product_id,customer_id,made_on) values (1,1,timestamp '2005-05-13 07:15:30 EST'); select * from purchases_with_local_tz;
2.时间戳函数
2.1 CURRENT_TIMESTAMP、LOCAL TIMESTAMP、SYSTIMESTAMP
select current_timestamp,localtimestamp,systimestamp from dual; alter session set time_zone = 'EST'; select current_timestamp,localtimestamp,systimestamp from dual; alter session set time_zone = 'PST';
2.2 EXTRACT({YEAR|MONTH|DAY|HOUR|MINUTE|SECOND}|
{TIMEZONE_HOUR|TIMEZONE_MINUTE}|{TIMEZONE_REGION|TIMEZONE_ABBR} FROM x)
--从x中提取并返回年、月、日、时、分、秒、时区,x可以是DATE或TIMESTAMP类型 select extract(year from to_date('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS')) as YEAR, extract(month from to_date('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS')) as MONTH, extract(day from to_date('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS')) as DAY from dual; select extract(hour from to_timestamp('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS')) as HOUR, extract(minute from to_timestamp('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS')) as MINUTE, extract(second from to_timestamp('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS')) as SECOND from dual; select extract(timezone_hour from to_timestamp_tz('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS TZH:TZM')) as TZH, extract(timezone_minute from to_timestamp_tz('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS TZH:TZM')) as TZM, extract(timezone_region from to_timestamp_tz('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS TZR')) as TZR, extract(timezone_abbr from to_timestamp_tz('05-JAN-2012 19:15:26','DD-MON-YYYY HH24:MI:SS TZR')) as TZA from dual;
2.3 FROM_TZ(x,time_zone)
--将timestamp类型的x转换为time_zone指定的时区,并返回timestamp with timezone类型的值 select from_tz(timestamp '2012-05-13 07:15:31.1234', '-7:00') from dual;
2.4 SYS_EXTRACT_UTC(x)
--将timestamp with timezone类型的x转换为包含UTC时区日期和时间的timestamp类型 select sys_extract_utc(timestamp '2012-11-17 19:15:26 PST') from dual; select sys_extract_utc(timestamp '2012-05-17 19:15:26 PST') from dual;
2.5 TO_TIMESTAMP(x[,format])
--将字符串x转换为timestamp类型,x可以为CHAR/VARCHAR2/NCHAR/NVARCHAR2类型 select to_timestamp('2012-05-13 07:15:31.1234','YYYY-MM-DD HH24:MI:SS.FF') from dual;
2.6 TO_TIMESTAMP_TZ(x[,format])
--将字符串x转换为timestamp with timezone类型 select to_timestamp_tz('2012-05-13 07:15:31.1234 PST','YYYY-MM-DD HH24:MI:SS.FF TZR') from dual; select to_timestamp_tz('2012-05-13 07:15:31.1234 -7:00','YYYY-MM-DD HH24:MI:SS.FF TZM') from dual;
2.7 CAST(x as type)
--将字符串x转换为timestamp with local time stamp类型 select cast('13-JUN-12' as timestamp with local time zone) from dual; select cast( to_timestamp_tz( '2012-05-13 07:15:31.1234 PST','YYYY-MM-DD HH24:MI:SS.FF TZR' ) as timestamp with local time zone ) from dual; select cast( to_timestamp_tz( '2012-05-13 07:15:31.1234 EST','YYYY-MM-DD HH24:MI:SS.FF TZR' ) as timestamp with local time zone ) from dual;
四、时间间隔(time interval)
1.时间间隔类型
1.1 interval year to month类型
--interverl '[+|-][y][-m] [year[(years_precision)]] [to month]' create table coupons ( coupon_id integer constraint coupons_pk primary key, name varchar2(30) not null, duration interval year(3) to month); insert into coupons (coupon_id,name,duration) values (1,'$1 off Z Files',interval '1' year); insert into coupons (coupon_id,name,duration) values (2,'$2 off Pop 3',interval '11' month); insert into coupons (coupon_id,name,duration) values (3,'$3 off Modern Science',interval '14' month); insert into coupons (coupon_id,name,duration) values (4,'$2 off Tank War',interval '1-3' year to month); insert into coupons (coupon_id,name,duration) values (5,'$1 off Chemistry',interval '0-5' year to month); insert into coupons (coupon_id,name,duration) values (6,'$2 off Creative Yell',interval '123' year(3));
1.2 interval day to second类型
--interval '[+|-][d] [h[:m[:s]]]' [day[(days_precision)]] to hour|minute|second[(second_precision)] create table promotions ( promotion_id integer constraint promotions_pk primary key, name varchar2(30) not null, duration interval day(3) to second(4)); insert into promotions (promotion_id,name,duration) values (1,'10% off Z Files',interval '3' day); insert into promotions (promotion_id,name,duration) values (2,'20% off Pop 3',interval '2' hour); insert into promotions (promotion_id,name,duration) values (3,'30% off Modern Science',interval '25' minute); insert into promotions (promotion_id,name,duration) values (4,'20% off Tank War',interval '45' second); insert into promotions (promotion_id,name,duration) values (5,'10% off Chemistry',interval '3 2:25' day to minute); insert into promotions (promotion_id,name,duration) values (6,'20% off Creative Yell',interval '3 2:25:45' day to second); insert into promotions (promotion_id,name,duration) values (7,'15% off My Front Line',interval '123 2:25:45.12' day(3) to second(2));
2.时间间隔函数
2.1 NUMTODSINTERVAL(x,interval_unit)
--将x转换为interval day to second类型 select numtodsinterval(1.5,'day'), numtodsinterval(3.25,'hour'), numtodsinterval(5,'minute'), numtodsinterval(10.123456789,'second') from dual;
2.2 numtoyminterval(x,interval_unit)
--将x转换为interval year to month类型 select numtoyminterval(1.5,'year'), numtoyminterval(3.25,'month') from dual;
【参考资料】
[1] Jason Price.精通Oracle Database 12c SQL&PLSQL编程(第3版).[M].北京:清华大学出版社,2014