OCP-1Z0-051-V9.02-93題
93. View the Exhibit and examine the structure of the CUSTOMERS table.
Using the CUSTOMERS table, y ou need to generate a report that shows an increase in the credit limit
by 15% for all customers. Customers whose credit limit has not been entered should have the message "
Not Available" displayed.
Which SQL statement would produce the required result?
A. SELECT NVL(cust_credit_limit,'Not Available')*.15 "NEW CREDIT"
FROM customers;
B. SELECT NVL(cust_credit_limit*.15,'Not Available') "NEW CREDIT"
FROM customers;
C. SELECT TO_CHAR(NVL(cust_credit_limit*.15,'Not Available')) "NEW CREDIT"
FROM customers;
D. SELECT NVL(TO_CHAR(cust_credit_limit*.15),'Not Available') "NEW CREDIT"
FROM customers;
Answer: D 1、NVL 官方解釋:Purpose
NVL
lets you replace null (returned as a blank) with a string in the results of a query.
If expr1
is null, then NVL
returns expr2
. Ifexpr1
is not null, then
NVL
expr1
.
如果expr1是null,則返回expr2,如果expr1 is not null,則返回expr1.
The arguments expr1
and expr2
can have any data type. If their data types are different, then Oracle Database implicitly converts one to the other.
If they cannot be converted implicitly, then the database returns an error.
expr1 and expr2
可以是任意的資料型別,但他們必須是同一資料型別,或者是隱式轉換為同一資料型別,又或者是顯示轉換為同一資料型別。
如果他們不是同一型別,則報錯。
The implicit conversion is implemented as follows:
-
If
expr1
is character data, then Oracle Database convertsexpr2
to the data type ofexpr1
before comparing them and returnsVARCHAR2
in the character set ofexpr1
. -
如果expr1 是字元型別,則expr2在比較前轉換為expr1的資料型別,在進行比較。
-
If
expr1
is numeric, 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是數字型別,則判斷哪個引數的資料型別高就隱式轉為哪個資料型別。
2、Data Conversion
此處考試的意圖在與NVL函式裡引數的資料型別的一致性以及資料型別之間的轉換。
由題意可知,先用同to_char函式顯示將cust_credit_limit*.15轉換為字元型別,才能達成資料型別一致。
A答案:
[email protected]> SELECT NVL(cust_credit_limit,'Not Available')*.15 "NEW CREDIT" FROM customers; SELECT NVL(cust_credit_limit,'Not Available')*.15 "NEW CREDIT" FROM customers * ERROR at line 1: ORA-01722: invalid numberB答案:
[email protected]> SELECT NVL(cust_credit_limit*.15,'Not Available') "NEW CREDIT" FROM customers; SELECT NVL(cust_credit_limit*.15,'Not Available') "NEW CREDIT" FROM customers * ERROR at line 1: ORA-01722: invalid numberC答案:
[email protected]> SELECT TO_CHAR(NVL(cust_credit_limit*.15,'Not Available')) "NEW CREDIT" FROM customers; SELECT TO_CHAR(NVL(cust_credit_limit*.15,'Not Available')) "NEW CREDIT" FROM customers * ERROR at line 1: ORA-01722: invalid numberD答案:
[email protected]> SELECT NVL(TO_CHAR(cust_credit_limit*.15),'Not Available') "NEW CREDIT" 2 FROM customers where rownum<5; NEW CREDIT ---------------------------------------- 225 1050 1650 225