oracle學習筆記單行函數

單行函數

只對一行進行變換  每行返回一個結果

單行函數分 字符、數值、日期、轉換、通用

字符函數:大小寫控制函數、字符控制函數

          大小寫控制函數:lower, upper, initcap

          字符控制函數:concat,substr,length,instr,lpad|rpad,trim,replace

lower,upper,initcap      

select lower('SQL') from dual;
--結果 sql
select upper('sql') from dual;
--結果 SQL
select initcap('SQL COurs') from dual;
--結果 Sql Cours 首字母大寫

concat,substr,length,instr,lapd|rpd,trim ,replace

select concat('hello','world') from dual; //結果 helloworld 
select substr('HelloWorld',1,4) from dual; //結果 Hell  從第一個字符開始取4個字符 
select length('hellowrld') from dual; //結果 9 求字符長度*/
select instr('Helloword','w') from dual; //結果 6 第一次出現W的位置
select lpad(salary ,10,'&') from employees ; //結果 &&&&&&2600 左填充& 
select rpad(salary ,10,'&') from employees ; //結果 2600&&&&&& 左填充& 
select trim('H' from 'HHllWoHldHH')from dual; //結果 llWoHld 去首尾不去中間

數字控制 round,trunc,mod

select round(45.36954,4) from dual; //  45.3695四捨五入
select trunc(45.36954,3) from dual; //  45.369 截斷
select mod(1600,300) from dual;     //   100 求餘數

日期控制  日期只可加減  months_betwwen,add_months,next_day,last_day

兩個日期相減返回日期之間相差的天數

可以用數字除24來向日期中加上或減去天數

--查詢公司中入職時間是每個月最後兩天的員工
select last_name,to_char(hire_date,'yyyy-mm-dd') hdate 
from employees 
where hire_date=last_day(hire_date)-1
--查詢到2005年入職超過5年的員工
select last_name,to_char(hire_date,'yyyy-mm-dd') from employees
where to_date('2005-12-31','yyyy-mm-dd')-hire_date >=5
下個月的今天(系統時間上加1個月)
select add_months(sysdate,1) from dual;
--兩天後的日期
select next_day(sysdate,2) from dual;

轉換  to_char,to_date,to_number

--隱式轉換
select '12'+3 from dual;   //char自動轉換爲number 加減
select sysdate +2 from dual; //number 自動轉換爲date
--顯式轉換
select last_name, to_char(hire_date,'yyyy-mm-dd') hire_date from employees;
select to_char(12345678.123,'999,999,999.99') from dual; // 12,345,678.12
select to_char(12345678.123,'000,000,999.99') from dual; // 012,345,678.12 沒有的們用0填充
select to_char(12345678.123,'L999,999,999.99') from dual; // $12,345,678.12 'L'爲當地貨幣 
select to_number('$12,345,678.12','L999,999,999.99') from dual; // 12345678.12
select to_number('12,345,678.12','999,999,999.99') from dual;  // 12345678.12

通用函數   這些函數適用於任何數據類型,同時也適用於空值

nvl(expr1,edpr2),nvl2(expr1,expr2,expr3),nullif(expr1,expr2),coalesce(expr1……exprn)

nvl 將空值轉換成一個已知的值 可用於日期、字符、數字

--求公司員工的年薪(含commission_pct)commisson_pct列中有空值
select last_name,salary*12*(1+nvl(commission_pct,0)) "nianxin" from employees;

--輸出last_name,department_id,當department_id爲null時,顯示‘沒有部門’
select last_name, nvl(to_char(department_id,'9999'),'沒有部門') Dep from employees;
select last_name, nvl(to_char(department_id),'沒有部門') Dep from employees; //簡寫
--NVL中的“沒有部門” 是char類型 要把department_id顯式轉換成爲NUMBER 使()中的數據類型一至

nvl2 (expr1,expr2,expr3) 當expr1不爲null 返回expr2 ,爲null返回expr3

--查詢員工的獎金率,若爲空,返回0.01,若不爲空,返回實際獎金率+0.015
select last_name,commission_pct ,nvl2(commission_pct,commission_pct + 0.015 ,0.01) 
from employees;

nullif (expr1,expr2)  兩個表達式相等返回NULL 不相等返回表達式1 expr1

select first_name,length(first_name) "expr1",
        last_name,length(last_name) "expr2",
        nullif(length(first_name),length(last_name)) result
from employees;

case 表達式


CASEexprWHENcomparison_expr1THENreturn_expr1

         [WHEN comparison_expr2 THEN return_expr2

          WHENcomparison_exprnTHENreturn_exprn

          ELSEelse_expr]

END

--查詢部門號爲10,20, 30 的員工信息, 若部門號爲
--10 則打印其工資的1.1 倍,
--20 號部門, 則打印其工資的1.2 倍, 
--30 號部門打印其工資的1.3 倍數
select last_name,department_id,case department_id when 10 then salary * 1.1
                                                  when 20 then salary * 1.2
                                                  else salary * 1.3 end  new_salary
from employees
where department_id in (10,20,30) 

--上面的加顯示其他的人的工資
select last_name,department_id,case department_id when 10 then salary * 1.1
                                                  when 20 then salary * 1.2
                                                  when 30 then salary * 1.3
                                                  else salary  end  new_salary
from employees

decode 

DECODE(col|expression, search1, result1 ,

           [, search2, result2,...,]

           [, default])                                                      ------ 用小括號代替 when  ……then

--上面一樣的題
select last_name,department_id,decode(department_id,10,salary * 1.1,
                                                    20,salary * 1.2,
                                                       salary * 1.3) new_salary
from employees
where department_id In (10,20,30)
--加顯其他員工工資
select last_name,department_id,decode(department_id,10,salary * 1.1,
                                                    20,salary * 1.2,
                                                    30,salary * 1.3,
                                                    salary) new_salary
from employees

下載本文用數據庫文件http://down.51cto.com/data/2336926

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