Which SELECT statement should you use to extract the year from the system date and display it in the format "1998"?

  1. SELECT TO_CHAR(SYSDATE,'yyyy') FROM dual;

  2. SELECT TO_DATE(SYSDATE,'yyyy') FROM dual;

  3. SELECT DECODE(SUBSTR(SYSDATE, 8), 'YYYY') FROM dual;

  4. SELECT DECODE(SUBSTR(SYSDATE, 8), 'year') FROM dual;


Correct Option: A
Explanation:

To solve this question, the user needs to know the SQL syntax for extracting specific parts of a date and formatting date values. The user must use the correct function to extract the year from the system date and format it as "1998".

Option A is correct. This option uses the TO_CHAR function to convert the system date to a character string in the format specified by the second argument ('yyyy' in this case). This function extracts the year from the system date and formats it as a 4-digit string. The output will be "1998".

Option B is incorrect. This option uses the TO_DATE function, which is used to convert a character string to a date value. In this case, the first argument is already a date value, so using TO_DATE is unnecessary. Also, the second argument ('yyyy') specifies the format of the output, not the input.

Option C is incorrect. This option uses the DECODE function to extract the year from the system date. However, the syntax is incorrect. DECODE requires at least three arguments: the first is the value to be compared, the second is the comparison value, and the third is the result if the comparison is true. In this case, the first argument (SUBSTR(SYSDATE, 8)) is missing a second argument to compare it to.

Option D is incorrect. This option also uses the DECODE function, but the syntax is incorrect. The SUBSTR function extracts a substring from the system date starting at position 8, which is the day of the month. There is no 'year' substring in this position, so the output will not be correct.

The Answer is: A. SELECT TO_CHAR(SYSDATE,'yyyy') FROM dual;

Find more quizzes: