欢迎投稿

今日深度:

【ORACLE】oracle时间对象的处理

【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;

 

www.htsjk.Com true http://www.htsjk.com/oracle/22944.html NewsArticle 【ORACLE】oracle时间对象的处理 1.1 oracle date对象的使用 (1) 创建date字段 例如, create table foo_7(d1 date); (2) 使用to_date函数插入date对象。to_date(字符串,字符串格式) 例如, insert into foo_7 va...
评论暂时关闭