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