See the structure of the PROGRAMS table:
Which two SQL statements would execute successfully? (Choose two.)
A.
SELECT NVL(ADD_MONTHS(END_DATE, 1), SYSDATE)
FROM programs;
B.
SELECT TO_DATE(NVL(SYSDATE-END_DATE, SYSDATE))
FROM programs;
C.
SELECT NVL(MONTHS_BETWEEN(start_date, end_date), ‘Ongoing’)
FROM programs;
D.
SELECT NVL(TO_CHAR(MONTHS_BETWEEN(start_date, end_date)), ‘Ongoing’) FROM programs;
Explanation:
NVL Function
Converts a null value to an actual value:
Data types that can be used are date, character, and number.
Data types must match:
– NVL(commission_pct, 0)
– NVL(hire_date, ’01-JAN-97′)
– NVL(job_id, ‘No Job Yet’)
MONTHS_BETWEEN(date1, date2): Finds the number of months between date1 and date2
. The result can be positive or negative. If date1 is later than date2, the result is positive; if date1 is earlier than
date2, the result is negative. The noninteger part of the result represents a portion of the month.
MONTHS_BETWEEN returns a numeric value. – answer C NVL has different datatypes – numeric and strings,
which is not possible!
The data types of the original and if null parameters must always be compatible. They must either be of the
same type, or it must be possible to implicitly convert if null to the type of the original parameter. The NVL
function returns a value with the same data type as the original parameter.
Eamon
September 21, 2015 at 12:56 pm
Point of note.
Be careful with B
Here is why …
This works …
SELECT NVL(SYSDATE-END_DATE,SYSDATE) FROM programs;
PROGRAMS is actually a table in the SYS schema so you can test it there (WARNING, no messing about in SYS, OK)
1 SELECT PROG_ID, SYSDATE, END_DATE, SYSDATE-END_DATE, NVL(SYSDATE-END_DATE,SYSDATE)
2 — , TO_DATE(NVL(SYSDATE-END_DATE,SYSDATE))
3 FROM programs
4* where PROG_ID !=4
SQL> /
PROG_ID SYSDATE END_DATE SYSDATE-END_DATE NVL(SYSDATE-END_DATE,SYSDATE)–,TO_DATE(NVL(SYSDATE-END_DATE,SYSDATE))
———- ——— ——— —————- ———————————————————————-
1 21-SEP-15 06-AUG-15 46.7854745 46.7854745
2 21-SEP-15 27-JUL-15 56.7854745 56.7854745
3 21-SEP-15 17-JUL-15 66.7854745 66.7854745
5 21-SEP-15 27-JUN-15 86.7854745 86.7854745
However this doesnt work
SELECT TO_DATE(NVL(SYSDATE-END_DATE,SYSDATE)) FROM programs;
because you cannot TO_DATE the number 46.7854745.
So the problem is this case is not due to NVL.