select ename,hiredate,sysdate from scott.emp ; 可以查詢出員工入職到現在的日期
select ename,sysdate-hiredate from scott.emp; 查看在職員工已經多少天;
select ename,months_between(sysdate,hiredate) from scott.emp 多少月
select add_months(sysdate,6) from scott.emp 系統時間加6個月的時間;
number_tochar:
select to_char(sysdate,'YYYY-MM-DD') from dual;
select ename,to_char(sal,'L99,999.99') from dual;
select ename,to_char(sal,'$00,000.00') from dual;
9:代表一個數
0:強制顯示一個零
$:放置一個浮點型美元符號
L:使用浮點型當地貨幣符號
date_to_char:
char_to_date:
select to_date('1990-09-05','YYYY-MM-DD') from dual;
char_to_number:
select to_number(‘$00,800.00','L00,000.00') from dual; 800
---------
select
to_char(sysdate,'yyyy') cyrr_year
to_char(to_date('07','yy'),'YYYY') yy07,
to_char(to_date('97','yy'),'YYYY') yy97,
to_char(to_date('07','rr'),'YYYY') rr07,
to_char(to_date('97','rr,'YYYY') rr97
from dual;
------------
實際上對於日期提供以下三種計算模式:
日期+數字=日期(若干天之后的日期)
select sysdate +10 from dual;
日期-數字=日期(若干天之前的日期)
select sysdate -10 from dual;
日期-日期=數字(兩個日期期間的天數)
計算每一位雇員到今天為止的雇佣天數。
select ename,hiredate, sysdate-hiredate form scott.emp
計算兩個日期見所經歷的月數總和。
語法:數字 MONTHS_BETWEEN(日期1,日期2)
MONTHS_BETWEEN函數返回兩個日期之間的月份數。
1:范例: 計算每一位員工到今天為止雇佣總年數。
select ename,hiredate,
trunc(months_between(sysdate,hiredate)/12) years from scott.emp
2:增加若干月之后的月日期;
范例:測試ADD_MONTHS函數
select add_months(sysdate,4) from dual;
3:計算還要差1年滿34年的雇佣日期的全部雇佣。
select * from scott.emp
where trunc(months_between(sysdate,hiredate)/12)=34 ;
4:計算指定日期所在月的最后一天
select last_day(sysdate) from dual;
5:查詢出所有雇佣所在月的倒數第二天被雇佣的雇員信息
每個雇員的雇佣日期是不一樣的,所有每一個雇佣日期所在月的倒數第二天也不一樣。
select ename,hiredate,last_day(hiredate),last_day(hiredate)-2 from scott.emp;
6:計算下一個周二
select next_day(sysdate,‘星期二’) from dual;
7:計算scott.emp表中雇佣的員工到目前為止的雇佣年份:
select empno,ename,hiredate, trunc(months_between(sysdate,hiredate)/12) year from scott.emp
8:綜合分析:要求查詢出雇員的編號、姓名、雇佣日期,以及每一位雇員到今天為止被雇佣的年數,月數天數。
假設下載的日期是:2016-03-08.
select empno,ename,hiredate,
trunc(months_between(sysdate,hiredate)/12) gear,
trunc(mod(months_between(sysdate,hiredate,)/12)) months,
trunc(sysdate-add_months(hiredate,months_between(sysdate,hiredate))) day
from scott.emp;