4

I am working on an assignment where I need to display the system date in a very specific way in SQL. The form is supposed to be as follows:

Day of the week with just the first letter capitalized, the month number in Roman Numerals (capitalized), the day of the month, the year spelled out in capital letters with anno domini, the day of the year from the Julian calendar with the phrase "Day of the year", number of seconds past midnight with the phrase "Seconds past midnight"

I reached up to the actual number of seconds past midnight, but I can't seem to add "Seconds past midnight" to it.

I have looked for syntax online and didn't find exactly what I needed. It's possible my search queries weren't worded correctly or that I didn't look far enough. My textbook isn't very clear to me on this, either. The only thing I've seen in there is an explanation of the difference between CURRENTDATE and SYSDATE.

This is what I have so far. I realize it may be entirely wrong. I have attempted it without the + and also without the quotes around the phrase.

SELECT TO_CHAR
    (SYSDATE, 'Dy, RM, D, YEAR AD, DDD "Day of the year", SSSSS "Seconds past midnight") "NOW"
     FROM DUAL;

I expected the output to say Fri, IV, 19, TWENTY NINETEEN AD, 109 Day of the year, 73829 Seconds past midnight but it's giving me the following error: ORA-01801: date format is too long for internal buffer

user147219
  • 355
  • 1
  • 4
  • 13

1 Answers1

4

You could cheat - concatenate the last hardcoded text to the result of TO_CHAR, instead of including it in the format model.

SELECT TO_CHAR(SYSDATE, 'Dy, RM, DD, YEAR AD, DDD "Day of the year ", SSSSS')
       || ' Seconds past midnight' "NOW"
FROM DUAL;



NOW
----------------------------------------------------------------------------------
Fri, IV  , 19, TWENTY NINETEEN AD, 109 Day of the year , 54236 Seconds past midnight

Note also that the third format element must be DD (you have D, which will not give you the two-digit day of the month, in this case 19).

Further clarification on the error you got: This is written in the Oracle documentation

https://docs.oracle.com/en/database/oracle/oracle-database/12.2/sqlrf/Format-Models.html#GUID-49B32A81-0904-433E-B7FE-51606672183A

The total length of a datetime format model cannot exceed 22 characters.

However, a quick count shows that the length of the format model in my solution is in fact more than 22 characters. It is not uncommon for Oracle documentation to be plain wrong. In this case, there seems to be a limit to the length of a format model, even though it is not the one in the documentation...