Oracle DB NVL、NVL2、NULLIF、COALESCE函數

來源:互聯網
上載者:User

  • 常規函數
下列函數可用於任何資料類型,且適合使用空值的場合:• NVL (expr1, expr2)• NVL2 (expr1, expr2, expr3)• NULLIF (expr1, expr2)• COALESCE (expr1, expr2, ..., exprn) 下列函數可用於任何資料類型,且適合運算式列表中使用空值的場合。 
  • NVL函數
將空值轉換為實際值:• 可以使用的資料類型為日期、字元和數字。• 資料類型必須匹配:– NVL(commission_pct,0)– NVL(hire_date,'01-JAN-97')– NVL(job_id,'NoJob Yet') 要將空值轉換為實際值,請使用NVL函數。文法NVL (expr1, expr2)在該文法中:• expr1是可能包含空值的源值或運算式• expr2是用於轉換空值的目標值可以使用NVL函數轉換任意資料類型,但是傳回值始終與expr1具有相同的資料類型。各種資料類型的NVL轉換 
  • 使用NVL函數
hr@TEST0924> SELECT last_name, salary,NVL(commission_pct, 0),(salary*12) + (salary*12*NVL(commission_pct, 0)) AN_SAL FROM employees;  LAST_NAME SALARY NVL(COMMISSION_PCT,0) AN_SAL------------------------- ---------- --------------------- ----------OConnell 2600 0 31200Khoo 3100 0 37200Vargas 2500 0 30000Russell 14000 .4 235200Bernstein 9500 .25 142500  要計算所有僱員的年度報酬,需要將月薪乘以12,然後再加上傭金百分比:hr@TEST0924> SELECT last_name, salary, commission_pct, (salary*12) + (salary*12*commission_pct) AN_SAL FROM employees;  LAST_NAME SALARY COMMISSION_PCT AN_SAL------------------------- ---------- -------------- ----------OConnell 2600Vargas 2500Russell 14000 .4 235200Taylor 3200 請注意,僅為那些領取傭金的僱員計算了年度報酬。如果運算式中的任何列值為空白,則結果也為空白。要計算所有僱員的年度報酬,必須將空值轉換為數字,然後才能應用算術運算子。在樣本中,NVL函數用於將空值轉換為零。

更多詳情見請繼續閱讀下一頁的精彩內容:

Oracle COALESCE函數  

Oracle 常用函數:nvl/NULLIF/case when/wm_concat/replace 

Oracle NULLIF函數

Oracle NULLIF函數的使用

Oracle nvl(),NVL2()函數介紹

Oracle中的NVL函數

關於Oracle資料庫中的NVL函數

  • 1
  • 2
  • 3
  • 下一頁

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.