06oracle學習筆記(03 單行函數)

--單行函數
/*
  字符函數
*/
--小寫轉大寫 upper()
select upper('hello accp!') from dual ;


--大寫轉小寫 lower()
select lower('HELLO ACCP!') from dual ;


--單詞首字母大寫,其餘小寫 initcap()
select initcap('hello accp!') from dual ;


--返回指定字符串的十進制數 ascii()
select ascii('\') ,ascii('n') n from dual ;


--返回指定整數對應的字符串 chr()
select chr(54740) zhao from dual ;

--字符串連接 concat()
select concat('0512-','88889999') from dual ;

--搜索指定字符串 instr(content,search,startindex,index)
select instr('hello accp welcome to you !','o',6,2) from dual ;

--返回字符串的長度 length()
select length('hello accp!') from dual ;

--粘貼字符 lpad(),rpad() 在左邊或右邊插入指定字符(根據指定返回結果長度循環插入指定字符)
select lpad(rpad('gao',10,'*'),17,'*') from dual ; --結果 *******gao*******
select rpad('hello',15,'accp') from dual ; --結果 helloaccpaccpac

--刪除字符串 ltrim() rtrim() trim()
select '  accp  ' str from dual ;
select ltrim('  accp  ') str from dual ;
select rtrim('  accp  ') str from dual ;
select ltrim(rtrim('  accp  ',' '),' ') str from dual ;
select trim(' ' from '  accp  ') str from dual ;

--截取字符串 substr(str,startIndex,length)
select substr('hello accp!',1,7) str1,substr('hello accp!',0,7) str2 from dual ; 
--結果一樣都是hello a

--查詢僱員姓名的最後三個字母
select substr(ename,-3,3) ename from emp ;

--字符串內容替換 replace()
select replace('  accp  ',' ') str from dual ;      --accp
select replace('  accp  ',' ','*') str from dual ;  --**accp**

/*
       數值函數
       
*/
--返回大於指定值的最小的的整數 ceil()
select ceil(68.49),ceil(-68.49) from dual ; --69,-68

--返回小於指定值的最大整數
select floor(68.49),floor(-68.49) from dual ; --68,-69

--四捨五入 round()
select round(789.536),round(789.536,2),round(789.536,-2) from dual ;  --790,789.54,800

--截斷小數位 trunc()
select trunc(789.536),trunc(789.536,2),trunc(789.536,-2) from dual ;  --789,789.53,700

--取餘mod()
select mod(10,3) from dual ; --1

--返回一個數值的符合 sign()
select sign(5),sign(0),sign(-5) from dual ;  --1,0,-1

/*
       日期函數
*/
--當前日期 sysdate
select sysdate from dual ;

--查詢10部門僱員進入公司的星期數
select floor((sysdate-hiredate)/7) "weeks" from emp ;

--求出給定日期範圍的月數 months_between()
select floor(months_between(sysdate,'27-2月 -83')) "months" from dual ;

--在指定的日期上加上指定的月數求出之後的日期
select add_months(sysdate,12) "new_date" from dual ;

--求出下一個指定星期對應的日期
select next_day(sysdate,'星期五') from dual ;

--求出指定日期的最後一天 last_day()
select last_day(sysdate) from dual ;


/*
       轉換函數
*/
--轉換成字符串 to_char()
/*
YYYY:四位表示的年份 
YYY,YY,Y:年份的最後三位、兩位或一位,缺省爲當前世紀 
MM:01~12的月份編號 
MONTH:九個字符表示的月份,右邊用空格填補 
MON:三位字符的月份縮寫 
WW:一年中的星期     
D:星期中的第幾天 
DY/DAY:顯示星期幾
DD:月份中的第幾天 
DDD:年所中的第幾天 
DAY:九個字符表示的天的全稱,右邊用空格補齊 
HH,HH12:一天中的第幾個小時,12進製表示法 
HH24:一天中的第幾個小時,取值爲00~23 
MI:一小時中的分鐘 
SS:一分鐘中的秒 
SSSS:從午夜開始過去的秒數 

*/
select to_char(hiredate,'yyyy/mm/dd') "date" from emp ;    --格式化日期到字符串
select to_char(hiredate,'fmyyyy/mm/dd') "date" from emp ;  --fm去前導零
select to_char(sysdate,'fmyyyy/mm/dd hh24:mi:ss') "date" from emp ;
select to_char(sal,'L99,999') "sal" from emp ;             --用當地貨幣表示法
select to_char(sal,'$99,999') "sal" from emp ;             --指定美元貨幣表示法
/*
to_char函數特殊用法 

to_char(sysdate,'d') 每週第幾天 
to_char(sysdate,'dd') 每月第幾天 
to_char(sysdate,'ddd') 每年第幾天 
to_char(sysdate,'ww') 每年第幾周 
to_char(sysdate,'mm') 每年第幾月 
to_char(sysdate,'q') 每年第幾季 
to_char(sysdate,'yyyy') 年 
*/
select to_char(sysdate,'ww') from dual ;   --今年第38周
--將字符串轉換成日期 to_date(string,'format') 
select to_date('2009-9-18','yyyy-mm-dd') from dual ;

--將字符串轉換成數字 to_number(string)
select to_number('123')+to_number('123') from dual ;

--1 查詢部門30中的所有員工
select * from emp where deptno=30;

--2 列出所有辦事員(CLERK)的姓名,編號和部門編號
select ename,empno,deptno from emp where lower(job)='clerk';

--3 找出佣金高於薪金的員工
/*
  truncate table emp ;
  select * from emp;
  insert into emp select * from scott.emp;
*/
select * from emp where comm > sal;

--求出每個僱員的年薪
/*
   nvl(arg,value) 如果前面的arg值爲null,那麼返回後面的value值    
*/
select (sal+nvl(comm,0))*12 income ,ename from emp;

--4 找出佣金高於薪金的60%的員工
select * from emp where comm>sal*0.6;

--5 找出部門10中所有經理(MANAGER)和部門20中所有辦事員(CLERK)的詳細資料
select * from emp
where (deptno=10 and lower(job)='manager') or (deptno=20 and lower(job)='clerk');

--6 找出部門10中所有經理(MANAGER),部門20中的所有辦事員(CLEAK),
--  既不是經理又不是辦事員但薪金大於或等於2000的所有員工的詳細資料
select * from emp
where (deptno=10 and lower(job)='manager') or 
      (deptno=20 and lower(job)='clerk') or
      (sal>=2000 and lower(job) not in ('manager','clerk'));

--7 找出收取佣金的員工的不同工作
/*
    null類型數據要用 is null和is not null來判斷,其它的任何判斷都是錯誤的
    如=null,<null等.
    select nvl(comm,0) from emp;
    select * from emp where comm is null;
    select * from emp where comm is not null ;
*/
select distinct job from emp
where comm is not null ;

--8 找出不收取佣金或收取佣金低於100的員工
select * from emp
where comm is null or comm<100;

--9 找出各月倒數第3天受僱的所有員工
select * from emp where last_day(hiredate)-2=hiredate ;

--10 找出早於12年前受僱的員工
select * from emp where months_between(sysdate,hiredate)/12 > 12 ;

--11 以首字母大寫的方式顯示所有員工的姓名
select upper(ename) from emp;

--12 顯示正好爲5個字符的員工的姓名
select ename from emp where ename like '_____';

--13 顯示不帶有"R"的員工的姓名
select ename from emp where ename not like '%R%';

--14 顯示所有員工的姓名的前三個字符
select substr(ename,1,3) ename from emp ;

--15 顯示所有員工的姓名,用"a"代替所有的"A"
select replace(ename,'A','a') ename from emp ;

--16 顯示滿10年服務年限的員工的姓名和受僱日期
select ename,hiredate from emp where months_between(sysdate,hiredate)/12 > 10 ;

--17 顯示員工詳細信息,按姓名排序
select * from emp order by ename;

--18 顯示員工的姓名和受僱日期,根據其服務年限,將最老的員工排在前面
select ename,hiredate from emp order by hiredate asc ;

--19 顯示所有員工的姓名,工作和薪金,按工作的降序排序,若工作相同則薪金排序
select ename,job,sal from emp order by job desc,sal;

--20 顯示所有員工的姓名,加入公司的年份和月份,按受僱日期所在月排序,若月份相同則將
--最早年份的員工排在前面
select ename,to_char(hiredate,'YYYY') year,to_char(hiredate,'MM') month from emp order by month,year;

--21 顯示在一個月爲30天的情況,所有員工的日薪金,忽略餘數
select round(sal/30) from emp ;

--22 找出在(任何年份的)2月受僱的所有員工
select * from emp where to_char(hiredate,'mm')=2;

--23 對於每個員工,顯示其加入公司的天數
select round(sysdate-hiredate) day_num,ename from emp;

--24 顯示姓名字段的任何位置包含"A"的所有員工的姓名
select ename from emp where ename like '%A%' ;

--25 以年月日的方式顯示所有員工的服務年限(大概)
select  trunc(temp.days/365) years,
        trunc((temp.days-trunc(temp.days/365)*365)/30) months,
        trunc(temp.days-(trunc(temp.days/365)*365+trunc((temp.days-trunc(temp.days/365)*365)/30)*30)) days
     from  (select ceil(sysdate-hiredate) days from emp ) temp ;


 

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