文章出處

NVL函數 Oracle/PLSQL中的一個函數。

  格式為:

  NVL( string1, replace_with)

  功能:如果string1為NULL,則NVL函數返回replace_with的值,否則返回string1的值。

  注意事項: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的擴展-NVL2

  Oracle在NVL函數的功能上擴展,提供了NVL2函數。

  NVL2(E1, E2, E3)的功能為:如果E1為NULL,則函數返回E3,若E1不為null,則返回E2。


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()