基本介紹
- 中文名:nvl函式
- 外文名:nvl
- 格式:NVL( string1, replace_with)
- 相似:ISNULL( string1, replace_with)
- 領域:程式設計
格式,功能,注意事項,用法,
格式
NVL( string1, replace_with)
功能
如果string1為NULL,則NVL函式返回replace_with的值,否則返回原來的值。
引申一下,此NVL的作用與SQLserver 中的 ISNULL( string1, replace_with) 一樣。
注意事項
string1和replace_with必須為同一數據類型,除非顯式的使用TO_CHAR函式。
例:NVL(TO_CHAR(numeric_column), 'some string') 其中numeric_column代指某個數字類型的值。
例:nvl(yanlei777,0) > 0
NVL(yanlei777, 0) 的意思是 如果 yanlei777 是NULL, 則取 0值
用法
通過查詢獲得某個欄位的合計值,如果這個值為null將給出一個預設的默認值
例如:
select nvl(sum(t.dwxhl),1)
from tb_jhde t
就表示如果sum(t.dwxhl) = NULL 就返回 1
另一個有關的有用方法
declare i integer
select nvl(sum(t.dwxhl),1) into i from tb_jhde t where zydm=-1這樣就可以把獲得的合計值存儲到變數
i中,如果查詢的值為null就把它的值設定為默認的1
oracle中:
select nvl(rulescore,0) from zwjc_graderule where rulecode='FWTD';
如果記錄中不存在rulecode ='FWTD'的數據.則查不出數據.
select nvl(rulescore,0) into rule_score from zwjc_graderule where rulecode='FWTD';會報查不到數據的錯
select nvl(sum(rulescore),0) from zwjc_graderule where rulecode='FWTD';
如果記錄中不存在rulecode ='FWTD'的數據.還是可以得到一行列名為nvl(rulescore,0),值為0的數據.
select nvl(sum(rulescore),0) into rule_score from zwjc_graderule where rulecode='FWTD'; 不會報錯
nvl函式可以作用的數據類型:
字元類型,日期類型。