设为首页 加入收藏

TOP

【ORACLE】oracle时间对象的处理
2015-07-24 11:34:40 来源: 作者: 【 】 浏览:4
Tags:ORACLE oracle 时间 对象 处理

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;

?

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇mybaits的xxMapper.xml文件中大于.. 下一篇enq:SQ-contention

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·哈希表 - 菜鸟教程 (2025-12-24 20:18:55)
·MySQL存储引擎InnoDB (2025-12-24 20:18:53)
·索引堆及其优化 - 菜 (2025-12-24 20:18:50)
·Shell 中各种括号的 (2025-12-24 19:50:39)
·Shell 变量 - 菜鸟教 (2025-12-24 19:50:37)