久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

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

共計(jì) 4081 個(gè)字符,預(yù)計(jì)需要花費(fèi) 11 分鐘才能閱讀完成。

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

單行函數(shù)

只對(duì)一行進(jìn)行變換   每行返回一個(gè)結(jié)果

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

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

          大小寫(xiě)控制函數(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  首字母大寫(xiě)

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  從第一個(gè)字符開(kāi)始取 4 個(gè)字符  
select length(hellowrld) from dual; // 結(jié)果  9  求字符長(zhǎng)度 */
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  截?cái)?
select mod(1600,300) from dual; // 100  求余數(shù)

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

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

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

-- 查詢(xún)公司中入職時(shí)間是每個(gè)月最后兩天的員工
select last_name,to_char(hire_date, yyyy-mm-dd) hdate 
from employees 
where hire_date=last_day(hire_date)-1
-- 查詢(xún)到 2005 年入職超過(guò) 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
下個(gè)月的今天(系統(tǒng)時(shí)間上加 1 個(gè)月)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 自動(dòng)轉(zhuǎn)換為 number  加減
select sysdate +2 from dual; //number  自動(dòng)轉(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  沒(méi)有的們用 0 填充
select to_char(12345678.123, L999,999,999.99) from dual; // $12,345,678.12  L 為當(dāng)?shù)刎泿? 
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ù)類(lèi)型,同時(shí)也適用于空值

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

nvl  將空值轉(zhuǎn)換成一個(gè)已知的值   可用于日期、字符、數(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 時(shí),顯示‘沒(méi)有部門(mén)’select last_name, nvl(to_char(department_id, 9999), 沒(méi)有部門(mén)) Dep from employees;
select last_name, nvl(to_char(department_id), 沒(méi)有部門(mén)) Dep from employees; // 簡(jiǎn)寫(xiě)
--NVL 中的“沒(méi)有部門(mén)”  是 char 類(lèi)型   要把 department_id 顯式轉(zhuǎn)換成為 NUMBER  使 () 中的數(shù)據(jù)類(lèi)型一至

nvl2 (expr1,expr2,expr3)  當(dāng) expr1 不為 null  返回 expr2,為 null 返回 expr3

-- 查詢(xún)員工的獎(jiǎng)金率,若為空,返回 0.01, 若不為空,返回實(shí)際獎(jiǎng)金率 +0.015
select last_name,commission_pct ,nvl2(commission_pct,commission_pct + 0.015 ,0.01) 
from employees;

nullif (expr1,expr2)  兩個(gè)表達(dá)式相等返回 NULL  不相等返回表達(dá)式 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  表達(dá)式

CASEexprWHENcomparison_expr1THENreturn_expr1

  [WHEN comparison_expr2 THEN return_expr2

  WHENcomparison_exprnTHENreturn_exprn

  ELSEelse_expr]

END

-- 查詢(xún)部門(mén)號(hào)為 10,20, 30  的員工信息,  若部門(mén)號(hào)為
--10  則打印其工資的 1.1  倍,
--20  號(hào)部門(mén),  則打印其工資的 1.2  倍, 
--30  號(hào)部門(mén)打印其工資的 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])                                                      —— 用小括號(hào)代替  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)真閱讀完這篇文章,希望丸趣 TV 小編分享的“oracle 如何使用單行函數(shù)”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持丸趣 TV,關(guān)注丸趣 TV 行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!

正文完
 
丸趣
版權(quán)聲明:本站原創(chuàng)文章,由 丸趣 2023-07-19發(fā)表,共計(jì)4081字。
轉(zhuǎn)載說(shuō)明:除特殊說(shuō)明外本站除技術(shù)相關(guān)以外文章皆由網(wǎng)絡(luò)搜集發(fā)布,轉(zhuǎn)載請(qǐng)注明出處。
評(píng)論(沒(méi)有評(píng)論)
主站蜘蛛池模板: 伊吾县| 桓台县| 枣阳市| 黄龙县| 巫山县| 花莲县| 拉孜县| 榆林市| 收藏| 裕民县| 贡山| 邵东县| 乐山市| 韶关市| 深水埗区| 读书| 农安县| 正定县| 同仁县| 泰来县| 高邑县| 翼城县| 威宁| 五常市| 阿克苏市| 常宁市| 丰原市| 斗六市| 宁海县| 高州市| 都兰县| 晋中市| 扎兰屯市| 上蔡县| 启东市| 巫溪县| 图们市| 蚌埠市| 大渡口区| 铅山县| 五峰|