oracle——SQL複習05

--返回季度 1 2 3 4 
select to_char(sysdate, 'Q') from dual;  
--返回本月第幾周  
SELECT TO_CHAR(SYSDATE, 'W') FROM DUAL;  
--DD 當月第幾天 
SELECT TO_CHAR(SYSDATE, 'DD') FROM DUAL; 
--D 周內第幾天 
SELECT TO_CHAR(SYSDATE, 'D') FROM DUAL; 
-- 周幾
SELECT TO_CHAR(SYSDATE, 'DY') FROM DUAL; 
--下週幾的時間
select next_day(sysdate,'星期四') from dual ; 
--返回兩個時間的年代數
select to_char(sysdate,'YYYY') - to_char(t.hiredate,'YYYY') year from emp t where t.empno = 7369 ;
--返回兩個時間精確的年數
select months_between(sysdate,t.hiredate)/12 year from emp t where t.empno = 7369 ;
--返回兩個時間的精確月份數
select months_between(sysdate,t.hiredate) month from emp t where t.empno = 7369 ;
--返回兩個時間的精確天數
select trunc(sysdate - t.hiredate) day from emp t where t.empno = 7369 ;
 




select * from ctl_01;  
select * from ctl_02;  
select * from ctl_01_bak; 
select * from ctl_02_bak; 

--create table ctl_01_bak as select * from ctl_01;

--merge into函數  匹配的數據就更新,不匹配的數據就添加
merge into ctl_01   
 using ctl_02  
 on (ctl_01.id = ctl_02.id)
when matched then  update set ctl_01.age = ctl_02.age 
when not matched then insert values(ctl_02.id,'HELLO',ctl_02.age);

Rollback; 

select * from ctl_01 t;  
--any 配合< <= 時 獲得的是最大值   
select * from ctl_02 t2 where t2.id <= any (select id from ctl_01);
--相當於
select * from ctl_02 t2 where t2.id <= (select max(id) from ctl_01);

-- select * from ctl_02 t2 where t2.id >= any (select id from ctl_01); 
--all 配合 < = 獲得的是最小值   
select * from ctl_02 t2 
where t2.id <= all (select id from ctl_01); 
-- select * from ctl_02 t2 where t2.id >= all (select id from ctl_01); 

--取偶數列
select * from ctl_02 t where mod(t.id,2) = 0 ;  



--大寫
select upper('hello world') from dual;
--小寫
select lower('HELLO WORLD') from dual; 
--首字母大寫
select initcap('hello world,nice!') from dual;  

--字段截取
select substr('hello world, nice!',7,5) from dual ;  
--字段連接
select concat('Hello ','World') col from dual;  
select 'Hello' || ' World' col from dual;  

--替換函數
select t.name, replace(t.name,'s','x') from ctl_01 t; 



--轉換函數nvl
select nvl('xzy',0) from dual; 
select nvl(null,0) from dual; 
--空值爲第三個值,非空值就替換成'xzy'
select nvl2(null,'xyz',0) from dual; 
select nvl2(123,'xyz',0) from dual ; 

--匹配函數case when  .. then . when .. then ..else.end 
select t.comm,
       case
         when nvl(t.comm, 0) = 0 then
          '沒有'
         when nvl(t.comm, 0) < 1000 then
          '一般'
         else
          '不錯'
       end comm
  from emp t;

--匹配函數 decode()  
select decode(sign(nvl(t.comm, 0)),  0, '沒', 1, '有獎金'),
       sign(nvl(t.comm, 0)),
       t.comm
  from emp t;

select decode(sign(nvl(t.comm, 0) - 1000), -1, '一般', 1, '不錯'), t.comm
  from (select decode(sign(nvl(t.comm, 0)), 0, '沒', 1, '有獎金') bak,
               sign(nvl(t.comm, 0)),
               t.comm
          from emp t) t
 where t.bak = '有獎金'
  
  
  
   

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