码迷,mamicode.com
首页 > 数据库 > 详细

【ORACLE】oracle时间对象的处理

时间:2015-01-27 15:07:07      阅读:209      评论:0      收藏:0      [点我收藏+]

标签:oracle date   timestamp   to_date   

1.1  oracle date对象的使用

(1)     创建date字段

例如,

create table foo_7(d1 date);

(2)     使用to_date函数插入date对象。to_date(‘字符串字符串格式’)

例如,

insert into foo_7 values(to_date(‘2015年11月20日13时20分10秒‘,‘yyyy"年"mm"月"dd"日"hh24"时"mi"分"ss"秒"‘));

或者:yyyy-mm-dd hh24:mi:ss

(3)     使用to_char函数输出date对象。To_char(date对象,字符串格式’)

例如,

insert into foo_7 values(to_date(‘2015年11月20日13时20分10秒‘,‘yyyy"年"mm"月"dd"日"hh24"时"mi"分"ss"秒"‘));

 

(4)     Last_day()函数为了计算所在月份的最后一天,大月小月

例如,

select to_char(last_day(sysdate),‘yyyy-mm-dd‘) from dual;

(5)     使用months_between计算两个时间间隔几个月

例如,

select months_between(sysdate,to_date(‘1989-05-05‘,‘yyyy-mm-dd‘))/12 years from dual;

(6)使用extract()函数获取时间对象的年月日,格式为extract(year[mounth][day] from 时间对象),例如

select extract(year from sysdate) from dual;

(7)使用trunc函数吧时分秒改为00:00:00

例如,

select to_char(trunc(sysdate),‘yyyy-mm-dd hh24:mi:ss‘) from dual;

(8)     使用round函数对时进行取舍(过了12时,则进1);

例如,

select to_char(round(sysdate),‘yyyy-mm-dd hh24:mi:ss‘) from dual;

(9)     least(date1,date2)返回比较古老久远的时间对象

 

1.2 oracle timestamp() 时间戳对象,更加精确。

(1)     创建timestamp字段

例如,

create table foo_8(time timestamp);

(2)     利用系统常量systimestamp对象插入一条记录,

例如,

insert into foo_8 values(systimestamp);

(3)     利用to_char对象显示Timestramp字段的时间

显示到秒

select to_char(time,‘yyyy-dd-mm hh24:mi:ss‘) "time" from foo_8;

2015-25-01 13:15:31

显示到任意精度的秒

         SQL> select to_char(time,‘yyyy-dd-mm hh24:mi:sssssssss‘) "time" from foo_8;

显示到毫秒,微秒等

select to_char(time,‘yyyy-dd-mm hh24:mi:ss.ss‘) "time" from foo_8;  

 

【ORACLE】oracle时间对象的处理

标签:oracle date   timestamp   to_date   

原文地址:http://blog.csdn.net/clark_xu/article/details/43193603

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!