oracle如何使用單行函數(shù)

這篇文章主要介紹了oracle如何使用單行函數(shù),具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

創(chuàng)新互聯(lián)公司是網(wǎng)站建設(shè)專家,致力于互聯(lián)網(wǎng)品牌建設(shè)與網(wǎng)絡(luò)營銷,專業(yè)領(lǐng)域包括成都做網(wǎng)站、網(wǎng)站制作、成都外貿(mào)網(wǎng)站建設(shè)、電商網(wǎng)站制作開發(fā)、微信平臺小程序開發(fā)、微信營銷、系統(tǒng)平臺開發(fā),與其他網(wǎng)站設(shè)計及系統(tǒng)開發(fā)公司不同,我們的整合解決方案結(jié)合了恒基網(wǎng)絡(luò)品牌建設(shè)經(jīng)驗和互聯(lián)網(wǎng)整合營銷的理念,并將策略和執(zhí)行緊密結(jié)合,且不斷評估并優(yōu)化我們的方案,為客戶提供全方位的互聯(lián)網(wǎng)品牌整合方案!

單行函數(shù)

只對一行進行變換  每行返回一個結(jié)果

單行函數(shù)分 字符、數(shù)值、日期、轉(zhuǎn)換、通用

字符函數(shù):大小寫控制函數(shù)、字符控制函數(shù)

          大小寫控制函數(shù):lower, upper, initcap

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

lower,upper,initcap     

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

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

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

數(shù)字控制 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 求余數(shù)

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

兩個日期相減返回日期之間相差的天數(shù)

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

--查詢公司中入職時間是每個月最后兩天的員工
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
下個月的今天(系統(tǒng)時間上加1個月)
select add_months(sysdate,1) from dual;
--兩天后的日期
select next_day(sysdate,2) from dual;

轉(zhuǎn)換  to_char,to_date,to_number

--隱式轉(zhuǎn)換
select '12'+3 from dual;   //char自動轉(zhuǎn)換為number 加減
select sysdate +2 from dual; //number 自動轉(zhuǎn)換為date
--顯式轉(zhuǎn)換
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'為當(dāng)?shù)刎泿?nbsp;
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

通用函數(shù)   這些函數(shù)適用于任何數(shù)據(jù)類型,同時也適用于空值

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

nvl 將空值轉(zhuǎn)換成一個已知的值 可用于日期、字符、數(shù)字

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

--輸出last_name,department_id,當(dāng)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顯式轉(zhuǎn)換成為NUMBER 使()中的數(shù)據(jù)類型一至

nvl2 (expr1,expr2,expr3) 當(dāng)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 倍數(shù)
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, search2, result1 ,

           [, search3, 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

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“oracle如何使用單行函數(shù)”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)站欄目:oracle如何使用單行函數(shù)
新聞來源:http://bm7419.com/article6/pcciog.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、面包屑導(dǎo)航、Google品牌網(wǎng)站設(shè)計、搜索引擎優(yōu)化全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站建設(shè)