咱們遇到過大多的狀況的需求是查詢結果中空轉爲0,這個能夠經過oracle的NVL()函數就能夠搞定。oracle
以前作報表客戶有個需求,查詢出結果爲0 要轉成空的,不顯示0函數
那麼在oracle有沒有現成函數能搞定呢?有的spa
一、方法1:NULLIF()函數code
1)介紹對象
NULLIF (expr1, expr2),若expr1和expr2相等,返回NULL;不相等,等返回expr1blog
2)例子class
--將 0 轉成 空 ,結果 空 --- select NULLIF('', to_char(0)) SexStr1 from dual;
結果select
3)實際應用方法
-- 將入庫通知單的入庫數量 將 0 轉成 空 --- select NULLIF('', to_char(Num)) InNum from Dxc_In_Notice;
二、方法2:用case when 判斷im
1)介紹
用法就不解釋了,跟C#的if else查很少。
2)例子
--將 0 轉成 空 ,結果 空 --- select (case to_char(0) when '1' then '男' when '2' then '女'else '' end) SexStr2 from dual;
3)實際應用
--將 0 轉成 空 ,結果 空 --- select (case to_char(Sex) when '1' then '男' when '2' then '女'else '' end) SexStr2 from EdsEmployee;
注意:NULLIF() 函數的參數類型要一致,case 對象和when對象也同樣。