1. 程式人生 > >ORACLE第七篇(日期函式)

ORACLE第七篇(日期函式)

一、日期兩種型別簡介

Date 和 timestamp(時間戳)
Date 包含資訊 century(世紀資訊) year 年 month 月 day 日 hour 小時 minute 分鐘 second 秒
Timestamp 一般用於日期時間要求非常精確的情況,精確到毫秒級;
insert into t_date values(1,sysdate,systimestamp);
二、常用函式

 date 型別的常用函式:
 //查詢系統日期兩種格式
select sysdate from dual;
select systimestamp from
dual;
Add_months 新增月份 select add_months(d1,2) from t_date where id=1; Last_day 返回指定日期月份的最後一天 select last_day(d1) from t_date where id=1; update t_date set d3=to_date('2016-12-20','YYYY-MM-DD') where id=1; update t_date set d3=to_date('2016-12-20 18:31:34','YYYY-MM-DD HH24:MI:SS') where id=1; Months_between 返回兩個日期的相差月數 select
months_between(d1,d3) from t_date where id=1;
select ceil(months_between(sysdate,to_date('2016-1-28','yyyy-mm-dd'))) from t_date where id=1; ------------------------------ 18 next_day 返回特定日期之後的一週內的日期:select next_day(d1,2) from t_date where id=1;//返回下一週星期一的日期 Trunc 擷取日期: select
trunc(d1,'YYYY') from t_date where id=1;
select trunc(d1,'MM') from t_date where id=1; select trunc(d1,'DD') from t_date where id=1; select trunc(d1,'HH') from t_date where id=1; select trunc(d1,'MI') from t_date where id=1; Extract 返回日期的某個域: select extract(year from sysdate) from dual; select extract(month from sysdate) from dual; select extract(day from sysdate) from dual; select extract(Hour from systimestamp) from dual;//返回的不是中國的時間 select extract(minute from systimestamp) from dual; select extract(second from systimestamp) from dual; To_char 將日期轉換成字串: select to_char(d1,'YYYY-MM-DD') from t_date where id=1; select to_char(d1,'YYYY-MM-DD HH24:MI:SS') from t_date where id=1; //查詢日期的最值的那一行的數值 select * from (select * from t_date order by d1) where id=1; select * from t_date where d1=(select min(d1) from t_date); //日期比較 select * from t_date where d1>to_date('2015-6-1','yyyy-mm-dd'); //比較年分 select * from t_date where extract(year from d1)>extract(year from to_date('2015-6-1','yyyy-mm-dd'));

補充EXISTS函式
我的理解是exists作為where條件時,先執行where的查詢語句在一個個進行EXISTS裡面的迴圈判斷,EXISTS裡面的where條件欄位需要指明哪個表的不然預設是EXISTS裡面sql語句from下面的表