oracle函數NVL,NVL2和NULLIF之間的區別和使用

來源:互聯網
上載者:User

標籤:添加   直接   img   用法   區別   日期類型   height   dual   技術分享   

oracle用這麼幾個函數,可以協助我們解決資料上null或0的處理

1、NVL()函數

 1.1)介紹:NVL(expr1,expr2),若expr1為null, 返回expr2; 不為null,返回expr1。  注意:兩者類型要一致

 1.2)用法:

--expr1為空白,返回expr2,結果:‘未知‘--select NVL(null,‘未知‘) SexType from dual;--expr1不為空白,返回expr1, 結果:1 --select NVL(‘1‘,‘2‘) SexType from dual;

 1.3)實際應用:

如:我們查詢表資料出現這種結果,某個欄位若為null,返回0

--未加函數時情況 --select A.UseFlag 使用標誌,A.NAME 單據名稱 from Dxc_Receive_Notice A

若欄位不為null,直接返回欄位本身

--加函數時情況,要求(field 為null,返回0 ;field 不為null,返回本身) --select NVL(A.UseFlag,‘0‘) 使用標誌,A.NAME 單據名稱 from Dxc_Receive_Notice A

 

 

2、NVL2 函數

2.1)介紹:NVL2 (expr1, expr2, expr3),若expr1不為NULL,返回expr2 ;為NULL,返回expr3。注意:expr2 和expr3 類型不同的話,expr3 會轉換為expr2 的類型

2.2)用法:

  --不為null時,返回expr2 ,結果:1  select NVL2(0,1,2) from dual; --為null時,返回expr3 ,結果:2 select NVL2(null,1,2) from dual;

2.3)實際應用

將查詢結果中欄位若為null,返回0

--未加函數時情況 --select A.UseFlag 使用標誌,A.NAME 單據名稱 from Dxc_Receive_Notice A

若欄位不為null,直接返回欄位本身

 --應用到實際查詢中,要求(field 為null,返回0; field 不為null,返回本身) select NVL2(A.UseFlag,A.UseFlag,‘0‘) 使用標誌,A.NAME 單據名稱 from Dxc_Receive_Notice A

 

3、NULLIF函數

3.1)介紹:NULLIF (expr1, expr2),若expr1和expr2相等,返回NULL;不相等,等返回expr1

3.2)用法:

--相等時,返回結果: null -- select NULLIF(1,1) from dual;  --不相等時,返回結果: 1 -- select NULLIF(1,2) from dual;

 3.3)實際應用

 將查詢結果中欄位為‘0001/1/1’,返回null

 --未加函數查詢結果 要求(將日期類型預設‘0001/1/1‘,改成null,不相等,返回本身日期)--- select A.RECEIVEDATE 收貨日期,A.NAME 單據名稱 from Dxc_Receive_Notice A

若欄位不為‘‘0001/1/1’,返回欄位本身

 --添加函數查詢結果,要求(將日期類型預設‘0001/1/1‘,改成null,不相等,返回本身日期)--  select NULLIF(A.RECEIVEDATE,TO_DATE(‘0001/1/1‘,‘yyyy-mm-dd hh24:mi:ss‘)) 收貨日期,A.NAME 單據名稱 from Dxc_Receive_Notice A

 

oracle函數NVL,NVL2和NULLIF之間的區別和使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.