Oracle中的decode函數、nvl函數、coalesce函數的用法 ccx原創


select   *,(case when SEX is "男" then 0 when SEX is "女"  then  1  else 2 end) as  sex from user;  

等價於  select decode(SEX,"男",0,"女",1,2)sex from user;


若查詢的某個字段爲null  但是顯示時  想顯示 其他值(以顯示0爲例),則可以
select *,nvl(fenshu,0) from score;  
等價於  select *,(if(fenshu is null) then 0 else fenshu end if)  fs from score;
若 性別只要2種情況  select *,(if(SEX.equals("男")) then 0 else 1 end if)  sex  from user;


coalesce(n1,n2)  若n1!=null  則  返回n1 否則  判斷  n2是否爲空   若不爲空則返回 n2  若都爲空 則  返回null
(coalesce (N1, N2, ...,Nn)依次參考各參數表達式,遇到非null值即停止並返回該值)
發佈了10 篇原創文章 · 獲贊 2 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章