105. Examine the structure of the PROMOS table:
name Null Type
PROMO_ID NOT NULL NUMBER(3)
PROMO_NAME VARCHAR2(30)
PROMO_START_DATE NOT NULL DATE
PROMO_END_DATE NOT NULL DATE
You want to display the list of promo names with the message 'Same Day' for promos that started and
ended on the same day.
Which query gives the correct output?
A. SELECT promo_name, NVL(NULLIF(promo_start_date, promo_end_date), 'Same Day') FROM promos;
B. SELECT promo_name, NVL(TRUNC(promo_end_date - promo_start_date), 'Same Day') FROM promos;
C. SELECT promo_name, NVL2(TO_CHAR(TRUNC(promo_end_date-promo_start_date)), NULL,'SameDay') FROM promos;
D. SELECT promo_name, DECODE((NULLIF(promo_start_date, promo_end_date)), NULL,'Same day') FROM promos;
Answer: D
答案解析:
A。NULLIF里面额数据类型为date与 'Same Day'字符串类型不匹配,错误。
B。TRUNC将小数部分截断,但类型不一致,错误。
C。TO_CHAR(TRUNC(promo_end_date-promo_end_date)),如果相等,TRUNC为0,to_char后,字符为0,即始终有值,NVL2返回第二个值,即NULL
sh@TEST0924> SELECT promo_name, NVL2(TO_CHAR(TRUNC(promo_end_date-promo_end_date)), NULL,'SameDay') FROM PROMOTIONS;
PROMO_NAME NVL2(TO
------------------------------ -------
NO PROMOTION #
newspaper promotion #16-108
post promotion #20-232
newspaper promotion #16-349
internet promotion #14-471