我們遇到過大多的情況的需求是查詢結果中空轉為0,這個可以通過oracle的NVL()函數就可以搞定。
之前做報表客戶有個需求,查詢出結果為0 要轉成空的,不顯示0
那么在oracle有沒有現成函數能搞定呢?有的
1、方法1:NULLIF()函數
1)介紹
NULLIF (expr1, expr2),若expr1和expr2相等,返回NULL;不相等,等返回expr1
2)例子
--將 0 轉成 空 ,結果 空 --- select NULLIF('', to_char(0)) SexStr1 from dual;
結果
3)實際應用
-- 將入庫通知單的入庫數量 將 0 轉成 空 --- select NULLIF('', to_char(Num)) InNum from Dxc_In_Notice;
2、方法2:用case when 判斷
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對象也一樣。