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 DATE
You want to generate a report showing promo names and their duration (number of days). If the
PROMO_END_DATE has not been entered, the message ‘ONGOING’ should be displayed.
Which queries give the correct output? (Choose all that apply.)
A.
SELECT promo_name, TO_CHAR(NVL(promo_end_date -promo_start_date,’ONGOING’)) FROM promos;
B.
SELECT promo_name,COALESCE(TO_CHAR(promo_end_date – promo_start_date),’ONGOING’) FROM promos;
C.
SELECT promo_name, NVL(TO_CHAR(promo_end_date -promo_start_date),’ONGOING’) FROM promos;
D.
SELECT promo_name, DECODE(promo_end_date
-promo_start_date,NULL,’ONGOING’,promo_end_date – promo_start_date) FROM promos;
E.
SELECT promo_name, decode(coalesce(promo_end_date,promo_start_date),null,’ONGOING’, promo_end_date – promo_start_date)
FROM promos;
Does anybody know what is wrong with answer E?
coalesce returns a non null value and since the promo_start_date is not null by constraint then there will be always non null value, while in case promo_end_date is null the difference between promo_end_date and promo_start_date will be null
Does anybody know why D is correct answer. According to Oracle documentation “The return data type must be the same as that of the first matching comparison item”. Futhermore returned values shoud have the same data types but in D answer ‘ONGOING’ and promo_end_date – promo_start_date have different data types, string and number accordingly.
Oracle automatically converts expr and each search value to the data type of the first search value before comparing. Oracle automatically converts the return value to the same data type as the first result. If the first result has the data type CHAR or if the first result is null, then Oracle converts the retuyrn value to the data type VARCHAR2
COPIED from the SQL language reference page 5-58
Then why is A incorrect? If Oracle automatically converts expr and each search value to the data type, why in A not correct?
You can not convert ‘ONGOING’ to number.