【ORACLE】oracle時間對象的處理,oracle對象處理

來源:互聯網
上載者:User

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

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.