1. 程式人生 > >oracle中null值相關函式彙總

oracle中null值相關函式彙總

nvl

這裡寫圖片描述
最常用的函式。它接收兩個引數。返回第一個非空值。若兩個引數都為空,返回null。

例:

select nvl(null, 9695) from dual;  --9695
select nvl(123, null) from dual;   --123
select nvl(123, 9695) from dual;   --123
select nvl(null, null) from dual;  --null

coalesce

這裡寫圖片描述
與nvl非常相似的功能,同樣是返回第一個不為空的引數。不過它可以接受更多的引數

select coalesce(1
, 3, 4, 5, 6, 7) from dual;

coalesce與nvl很像,官方文件中也這樣說

This function is a generalization of the NVL function.

不過需要注意的是,coalesce對引數型別的要求更嚴格。它無法向nvl一樣最大限度的自動轉換資料型別。

select nvl(1, '2') from dual;     --沒問題
select coalesce(1, '2') from dual; --報錯

nvl2

這裡寫圖片描述

NVL2 lets you determine the value returned by a query based on whether a specified expression is null or not null. If expr1 is not null, then NVL2 returns expr2. If expr1 is null, then NVL2 returns expr3.

就是個三目運算子啦。如果第一個引數非空,返回第二個引數,否則返回第三個引數。

nullif

這裡寫圖片描述

NULLIF compares expr1 and expr2. If they are equal, then the function returns null. If they are not equal, then the function returns expr1. You cannot specify the literal NULL for expr1.

如果兩個引數相同,返回null,否則返回第一個引數。
意外的很有用的一個函式,具體見這篇部落格

oracle 使用nullif解決除數為零的問題

lnnvl

這裡寫圖片描述

LNNVL provides a concise way to evaluate a condition when one or both operands of the condition may be null. The function can be used in the WHERE clause of a query, or as the WHEN condition in a searched CASE expression. It takes as an argument a condition and returns TRUE if the condition is FALSE or UNKNOWN and FALSE if the condition is TRUE. LNNVL can be used anywhere a scalar expression can appear, even in contexts where the IS [NOT] NULL, AND, or OR conditions are not valid but would otherwise be required to account for potential nulls.
Oracle Database sometimes uses the LNNVL function internally in this way to rewrite NOT IN conditions as NOT EXISTS conditions. In such cases, output from EXPLAIN PLAN shows this operation in the plan table output. The condition can evaluate any scalar values but cannot be a compound condition containing AND, OR, or BETWEEN.

簡單來說的話,就是個取反的操作。比如1 > 2 返回true、1 = 1 返回false。
咋一看挺沒用的,但是別忘了在oracle中與null的比較全都返回false的特性。

with tab1 as (
select 1 id from dual union all
select 3 from dual union all
select null from dual 
)
select * from tab1 where id < 2
;
--當我想獲取id小於2的值時,直接id < 2即可
--但是當我想獲取id < 2且id為空的值時,則可以這麼寫
with tab1 as (
select 1 id from dual union all
select 3 from dual union all
select null from dual 
)
select * from tab1 where lnnvl(id > 2)
;

也就是說,在功能上lnnvl(id > 2)id < 2 or id is null 是等效的。
不過,文件中有一句話非常令人在意

Oracle Database sometimes uses the LNNVL function internally in this way to rewrite NOT IN conditions as NOT EXISTS conditions. In such cases, output from EXPLAIN PLAN shows this operation in the plan table output.

難道說lnnvl更有效率優勢?看來要找個時間研究下。

nanvl

這裡寫圖片描述

The NANVL function is useful only for floating-point numbers of type BINARY_FLOAT or BINARY_DOUBLE. It instructs Oracle Database to return an alternative value n1 if the input value n2 is NaN (not a number). If n2 is not NaN, then Oracle returns n2.
This function takes as arguments any numeric data type or any nonnumeric data type that can be implicitly converted to a numeric data type. Oracle determines the argument with the highest numeric precedence, implicitly converts the remaining arguments to that data type, and returns that data type.

只用於BINARY_FLOAT or BINARY_DOUBLE 的函式。這個真沒用過。
我執行了官方文件中的示例程式碼,但是一直報錯,求大神來解答。

CREATE TABLE float_point_demo
  (dec_num NUMBER(10,2), bin_double BINARY_DOUBLE, bin_float BINARY_FLOAT);

INSERT INTO float_point_demo
  VALUES (0,'NaN','NaN');

SELECT bin_float, NANVL(bin_float,0)
  FROM float_point_demo;

資料庫版本
Oracle Database 10g Enterprise Edition Release 10.2.0.5.0 - 64bi
Oracle Database 11g Enterprise Edition Release 11.2.0.4.0 - 64bit Production

相關文件位置
NANVL

TO_BINARY_DOUBLE