oracle單表函數查詢

1. 字符串轉小寫lower(員工姓名)


select lower(ename) from emp;


2. 首字母轉大寫initcap(員工姓名)


select initcap(ename) from emp;


3. 字符串取長度length


select length('length') from dual;


4. 字符串替換replace(員工姓名A-_)


select replace(ename,'A','_') from emp;




5. 字符串截取substr(員工姓名前三位)


select ename,substr(ename,1,3) from emp;


select ename,substr(ename,0,3) from emp;


select ename,substr(ename,-1,3) from emp;


6. 兩邊去空格trim(員工姓名)


select trim(ename) from emp;


7. 四捨五入round(953.6286,0,1,2,-1,-2)
ROUND(數字 | 列 [,保留小數的位數])


select round(953.6286) from dual;   954


select round(953.6286,0) from dual; 954


select round(953.6286,1) from dual; 953.6


select round(953.6286,2) from dual; 953.63


select round(953.6286,-1) from dual; 950


select round(953.6286,-2) from dual; 1000




8. 捨棄內容trunc(953.6286,0,1,2,-1,-2)
TRUNC(數字 | 列 [,保留小數的位數])




select round(953.6286) from dual;   953


select round(953.6286,0) from dual; 953


select round(953.6286,1) from dual; 953.6


select round(953.6286,2) from dual; 953.62


select round(953.6286,-1) from dual; 950


select round(953.6286,-2) from dual; 900


9. 取模mod(10,3)取餘數:MOD(數字 1,數字2)


select mod(10,3) from dual;


10. 獲取當前時間sysdate


select sysdate from dual;


11. 表示若干天之後的日期+3


select sysdate+3 from dual;


12. 兩個日期間的天數差sysdate-hiredate


select ename,hiredate,sysdate-hiredate from emp;


select trunc(sysdate-a.hiredate) from emp a;


13. 本月的最後一天日期last_day


select last_day(sysdate) from dual;


14. 兩個日期間的月份差months_between


select ename,hiredate,trunc(months_between(sysdate,hiredate))
 from emp;
 
 
15. 求出四個月後的日期
 
  select add_months(sysdate,4) from dual;
  
16. 日期變爲字符串to_char(mi,hh24,yyyy,day)


 select to_char(sysdate,'yyyy-mm-dd hh:mi:ss') from dual;
 
17. 數字變爲字符串to_char


SELECT TO_CHAR(89078907890,'L999,999,999,999,999') FROM dual;




18. 字符串轉日期to_date(yyyy-mm-dd hh:mi:ss)


SELECT TO_DATE('2017-09-07 11:33:22','yyyy-mm-dd hh:mi:ss') FROM dual;


19. 字符串轉數字TO_NUMBER(‘123’+4)TO_NUMBER 可以把字符串轉換成數值,在oracle中是自動轉換的


select '123' + 4 from dual;




20. DECODE()多值判斷


查詢員工和領導的薪資等級

select e.ename,e.job,e.sal,m.ename,d.dname,
decode(s1.grade,1,'第一級',2,'第二級',3,'第三級',4,'第四級','第五級'),
decode(s2.grade,1,'第一級',2,'第二級',3,'第三級',4,'第四級','第五級')
 from emp e
left join emp m on e.mgr=m.empno
left join dept d on e.deptno=d.deptno
left join salgrade s1 on e.sal between s1.losal and s1.hisal
left join salgrade s2 on m.sal between s2.losal and s2.hisal

21. case when條件判斷
case 列名 
when ‘匹配值’ then ‘返回值’
when ‘匹配值’ then ‘返回值’
Else ‘返回值’
End


select empno,ename,job,case job when 'CLERK' then '辦事員'

when 'SALESMAN' then '銷售人員'

else '其他'

end

from emp;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章