96.Examine the structure of the INVOICE table.
name Null Type
INV_NO NOT NULL NUMBER(3)
INV_DATE DATE
INV_AMT NUMBER(10,2)
Which two SQL statements would execute successfully? (Choose two.)
A.SELECT inv_no,NVL2(inv_date,'Pending','Incomplete') FROM invoice;
B.SELECT inv_no,NVL2(inv_amt,inv_date,'Not Available') FROM invoice;
C.SELECT inv_no,NVL2(inv_date,sysdate-inv_date,sysdate) FROM invoice;
D.SELECT inv_no,NVL2(inv_amt,inv_amt*.25,'Not Available') FROM invoice;
答案:AC
解析:
nvl2是干啥的参考95题
类型参看下面的
The argument expr1 can have any data type. The arguments expr2 and expr3 can have any data types except LONG.
If the data types of expr2 and expr3 are different, then Oracle Database implicitly converts one to the other. 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.
•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
上面主要就是说了,expr1可以是任意类型,expr2和expr3可以是除了long外的任何类型,如果expr2和expr3类型不一致,会尝试将一个进行隐士转换为另外一个,如果转不了那就报错
如果expr2是字符或者数字类型,那么应该按照下面2条规则
1.如果expr2是字符类型,那么回将expr3转化为expr2的类型,如果expr3是null那么就不用转了,直接返回空字符串
2.如果expr2是数字类型,那么oracle决定哪个参数具有高的数字优先级,那么就会将低的转换为高的
--上面说了这么多,其实就按照如果expr2和expr3类型不一致那么将expr3转换为expr2,如果expr3为null的话
--就不用管了
--第二条这个的意思,我感觉应该是如果都是number或者number字类型,比如
number,numeric
dec,decimal
double precision, float
real
int,integer,smallint
这样的下面这几个都是上面number的自己,都按照number转就行了
A:正确,两个一样
B:错误,'Not Available'转换成日期
C:正确,这个测试是可以的
SQL> select nvl2(null,sysdate-(sysdate-1),sysdate) from dual;
NVL2(NULL,SYSDATE-(SYSDATE-1),SYSDATE)
--------------------------------------
2457451.74
--这里涉及到两个日期相互减,oracle文档上说两个日期想减是数字型,其实这里并不是简单的数字型,
--比如
SQL> select nvl2(null,1,sysdate) from dual;
select nvl2(null,1,sysdate) from dual
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
--说明这里sysdate并不是简单的数字
SQL> select date '2016-03-07' - date '2016-03-06',dump(date '2016-03-07' - date '2016-03-06'),dump(2-1) from dual;
DATE'2016-03-07'-DATE'2016-03-06' DUMP(DATE'2016-03-07'-DATE'20
--------------------------------- -----------------------------
DUMP(2-1)
------------------
1 Typ=14 Len=8: 1,0,0,0,0,0,0,0
Typ=2 Len=2: 193,2
--他们一个类型是14,一个是12,具体的oracle内置数据类型如下
http://docs.oracle.com/cloud/latest/db112/SQLRF/sql_elements001.htm#BABCGCHG
D:错误,转不了
name Null Type
INV_NO NOT NULL NUMBER(3)
INV_DATE DATE
INV_AMT NUMBER(10,2)
Which two SQL statements would execute successfully? (Choose two.)
A.SELECT inv_no,NVL2(inv_date,'Pending','Incomplete') FROM invoice;
B.SELECT inv_no,NVL2(inv_amt,inv_date,'Not Available') FROM invoice;
C.SELECT inv_no,NVL2(inv_date,sysdate-inv_date,sysdate) FROM invoice;
D.SELECT inv_no,NVL2(inv_amt,inv_amt*.25,'Not Available') FROM invoice;
答案:AC
解析:
nvl2是干啥的参考95题
类型参看下面的
The argument expr1 can have any data type. The arguments expr2 and expr3 can have any data types except LONG.
If the data types of expr2 and expr3 are different, then Oracle Database implicitly converts one to the other. 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.
•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
上面主要就是说了,expr1可以是任意类型,expr2和expr3可以是除了long外的任何类型,如果expr2和expr3类型不一致,会尝试将一个进行隐士转换为另外一个,如果转不了那就报错
如果expr2是字符或者数字类型,那么应该按照下面2条规则
1.如果expr2是字符类型,那么回将expr3转化为expr2的类型,如果expr3是null那么就不用转了,直接返回空字符串
2.如果expr2是数字类型,那么oracle决定哪个参数具有高的数字优先级,那么就会将低的转换为高的
--上面说了这么多,其实就按照如果expr2和expr3类型不一致那么将expr3转换为expr2,如果expr3为null的话
--就不用管了
--第二条这个的意思,我感觉应该是如果都是number或者number字类型,比如
number,numeric
dec,decimal
double precision, float
real
int,integer,smallint
这样的下面这几个都是上面number的自己,都按照number转就行了
A:正确,两个一样
B:错误,'Not Available'转换成日期
C:正确,这个测试是可以的
SQL> select nvl2(null,sysdate-(sysdate-1),sysdate) from dual;
NVL2(NULL,SYSDATE-(SYSDATE-1),SYSDATE)
--------------------------------------
2457451.74
--这里涉及到两个日期相互减,oracle文档上说两个日期想减是数字型,其实这里并不是简单的数字型,
--比如
SQL> select nvl2(null,1,sysdate) from dual;
select nvl2(null,1,sysdate) from dual
*
ERROR at line 1:
ORA-00932: inconsistent datatypes: expected NUMBER got DATE
--说明这里sysdate并不是简单的数字
SQL> select date '2016-03-07' - date '2016-03-06',dump(date '2016-03-07' - date '2016-03-06'),dump(2-1) from dual;
DATE'2016-03-07'-DATE'2016-03-06' DUMP(DATE'2016-03-07'-DATE'20
--------------------------------- -----------------------------
DUMP(2-1)
------------------
1 Typ=14 Len=8: 1,0,0,0,0,0,0,0
Typ=2 Len=2: 193,2
--他们一个类型是14,一个是12,具体的oracle内置数据类型如下
http://docs.oracle.com/cloud/latest/db112/SQLRF/sql_elements001.htm#BABCGCHG
D:错误,转不了
本文介绍了 Oracle 数据库中的 INVOICE 表结构,并探讨了 NVL2 函数的使用。成功执行的 SQL 语句包括:A. 从 INVOICE 表中选择 INV_NO 和根据 INV_DATE 显示 'Pending' 或 'Incomplete';C. 使用 NVL2 函数结合当前日期分析 INV_DATE 字段。

2050

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



