OCP-1Z0-051-V9.02-95题

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.

Answer: A
 
答案解析:
此题考的本意是NVL和NVL2之间逻辑计算的区别。
 
1、NVL
2、NVL2
 

Purpose

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 expr2 is character data, then Oracle Database converts expr3 to the data type of expr2 before returning a value unless expr3 is a null constant.

  • In that case, a data type conversion is not necessary, and the database returns VARCHAR2 in the character set of expr2.

  • 如果expr2 是字符类型,则将expr3 转换为expr2相同的数据类型。

  • If expr2 is 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、创建表,插入数据。

sys@TEST0910> create table products
  2  (prod_id number(4) not null,
  3  prod_name varchar2(25),
  4  prod_expiry_date date);
 
Table created.
 
sys@TEST0910> insert into products values(1,'tomato',sysdate);
 
1 row created.
 
sys@TEST0910> select * from products;
 
   PROD_ID PROD_NAME                 PROD_EXPI
---------- ------------------------- ---------
         1 tomato                    18-SEP-13
 
2、开始测试:
sys@TEST0910> SELECT prod_id, NVL2(prod_expiry_date, prod_expiry_date + 15,'') from products;
 
   PROD_ID NVL2(PROD
---------- ---------
         1 03-OCT-13                                             
prod_expiry_date非空,则返回prod_expiry_date + 15
 
sys@TEST0910> SELECT prod_id, NVL(prod_expiry_date, prod_expiry_date + 15) from products;
 
   PROD_ID NVL(PROD_
---------- ---------
         1 18-SEP-13
prod_expiry_date非空,则返回prod_expiry_date
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值