95. The PRODUCTS table has the following structure:
name Null Type
PROD_ID NOT NULL NUMBER(4)
PROD_NAME VARCHAR2(25)
PROD_EXPIRY_DATE DATE
Evaluate the following two SQL statements:
SQL>SELECT prod_id, NVL2(prod_expiry_date, prod_expiry_date + 15,'') 如果prod_expiry_date为null,则返回空,否则返回prod_expiry_date + 15
FROM products;
SQL>SELECT prod_id, NVL(prod_expiry_date, prod_expiry_date + 15) 如果prod_expiry_date为null,则返回prod_expiry_date + 15,否则返回prod_expiry_date
FROM products;
Which statement is true regarding the outcome?
A. Both the statements execute and give different results.
B. Both the statements execute and give the same result.
C. Only the first SQL statement executes successfully.
D. Only the second SQL statement executes successfully.
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.
如果expr1非空,则返回 expr2,如果expr1是空值,则返回expr3
The argument expr1 can have any data type. The arguments expr2 and expr3 can have any data types except LONG.
expr1 可以是任意数据类型, expr2 and expr3 可以是任意数据类型,但不能是LONG类型,且数据类型要一致,或者隐式转换为一致,或者显示转换为一致。
If the data types of expr2 and expr3 are different, then Oracle Database implicitly converts one to the other.
如果expr2 and expr3 数据类型不同,则隐式转为相同
If they cannot be converted implicitly, then the database returns an error.
如果不能隐式转换,则报错。
If expr2 is character or numeric data, then the implicit conversion is implemented as follows:
-
If
expr2is character data, then Oracle Database convertsexpr3to the data type ofexpr2before returning a value unlessexpr3is a null constant. -
In that case, a data type conversion is not necessary, and the database returns
VARCHAR2in the character set ofexpr2. -
如果expr2 是字符类型,则将expr3 转换为expr2相同的数据类型。
-
If
expr2is numeric data, then Oracle Database determines which argument has the highest numeric precedence, implicitly converts the other argument to that data type, and returns that data type.
官方参考:http://docs.oracle.com/cd/E11882_01/server.112/e41084/functions120.htm#sthref1315
搭建环境:
1、创建表,插入数据。

本文通过实例对比了Oracle SQL中NVL与NVL2函数的功能差异,特别是当处理NULL值时的不同行为。通过创建表并插入数据,演示了两种函数在不同情况下的输出结果。

1万+

被折叠的 条评论
为什么被折叠?



