Previous | Table of Contents | Next

Page 157

Table 7.7. Date format elements.

Format Element Description
BC, B.C. BC indicator, which can be used with or without the periods.
AD, A.D. AD indicator, which can be used with or without the periods.
CC, SCC Century code. Returns negative value if using BC with SCC format.
SYYYY, YYYY Four-digit year. Returns negative value if using BC with SYYYY format.
IYYY Four-digit ISO year.
Y,YYY Four-digit year with a comma inserted.
YYY, YY, Y The last three, two, or one digits of the year. The default is the current century.
IYY, IY, I The last three, two, or one digits of the ISO year. The default is the current century.
YEAR, SYEAR Returns the year spelled out. SYEAR returns a negative value if using BC dates.
RR Last two digits of year in prior or future centuries.
Q Quarter of the year, values 1 to 4.
MM The month number from 01 to 12, Jan=01, Feb=02, and so on.
MONTH The month name always allocated to nine characters, right-padded with blanks.
MON The month name abbreviated to three characters.
RM Roman numeral representation of the month, values I to XII.
WW The week in the year, values 1 to 53.
IW The ISO week in the year, values 1 to 52 or 1 to 53.
W The week in the month, values 1 to 5. Week 1 begins on the first day of the month.
D The day of the week, values 1 to 7.
DD The day of the month, values 1 to 31.
DDD The day of the year, values 1 to 366.
DAY The name of the day spelled out, always occupying nine characters, right space padded.
DY Abbreviated name of the day to two characters.
                                                          continues

Page 158

Table 7.7. continued

Format Element Description
J Julian day counted since January 1, 4712 BC.
HH, HH12 The hour of the day, values 1 to 12.
HH24 The hour of the day, values 0 to 23.
MI The minute of the hour, values 0 to 59.
SS The second of the minute, values 0 to 59.
SSSS How many seconds past midnight, values 0 to 86399 (60 minutes/hr * 60 seconds/minute * 24 hours = 86400 seconds).
AM, A.M. The ante meridiem indicator for morning, with or without the periods.
PM, P.M. The post meridiem indicator for evening, with or without the periods.
Punctuation All punctuation passed through to a maximum of 220 characters.
Text All text passed through to a maximum of 220 characters.
TH Suffix to convert numbers to ordinal format, so 1 would be 1st, 2 would be 2nd, and so on. Always returns value in English language only.
SP Converts a number to its spelled format so 109 becomes one hundred nine. Always returns value in English language only.
SPTH Spells out numbers converted to ordinal format, so 1 would be FIRST, 2 would be SECOND, and so on. Always returns value in English language only.
FX Uses exact pattern matching between data element and the format.
FM Fill Mode: Toggles suppression of blanks in output from conversion.

Finally, the last part of the TO_DATE function is NLS_DATE_LANGUAGE. For all you network gurus, this is simply the language you want returned, such as English, Spanish, and so on. Remember, certain functions will only return values in the English language, such as SPTH, SP, and so forth.

Just to confuse you even further, take a look at the syntax of the TO_DATE function using Julian days. Again, this is the number of days that have elapsed since January 1, 4712 BC.

Page 159

TO_DATE(number, format, NLS_Params)

The syntax is not much different than the previous syntax for normal character-based dates, except that you are now passing to the function a number value that represents the Julian days.

As you can see, this simple function can be formatted in many different ways. The best way is to go ahead and type in all the following listings to see your output. These examples use SQL*Plus as a quick method for testing, but these could easily be used in your PL/SQL code except where specified as SQL only. Go ahead and enter and execute Listings 7.1 and 7.2.

INPUT
Listing 7.1. Converting number representation to DATE format.

SELECT TO_DATE(`061167','MMDDYY') "Birthday" from DUAL;

OUTPUT
The output will appear as

Birthday
--------
11-JUN-67

INPUT
Listing 7.2. Converting spelled date to DATE format.

SELECT TO_DATE(`January 15','MONTH DD') "Sample" from DUAL;

OUTPUT
Your output should appear similar to the following:

Sample
--------
15-JAN-97
ANALYSIS Notice that even though the example did not specify the century or the year, it takes the default system century and year.

What are some of the possible errors that you can encounter with TO_DATE? What if you leave off the mask, or incorrectly specify the mask? Listing 7.3 reflects a sample error.

INPUT
Listing 7.3. Errors with TO_DATE.

SELECT TO_DATE(`061167') "Error" from DUAL;

Page 160

OUTPUT
You should get the error message

ERROR:
ORA-01843: not a valid month


no rows selected

Because you did not apply a mask, the standard date mask was applied from the format DD-MON-YY. Because 06 is a valid day, Oracle had no problems with handling of the day. However, when the program arrives at the month, the default date used the three-letter abbreviation for the month. The value 116 is not a valid abbreviation for a month. Listing 7.4 shows an instance when the default date can be used.

INPUT
Listing 7.4. Proper use of default format mask.

SELECT TO_DATE('06-Jan-67') "Correct" from DUAL;

OUTPUT
The output will appear as

Correct
--------
06-JAN-67

As you can see, making sure that you pass the format mask in the same manner as the character string is highly important!

The next example demonstrates using TO_DATE as part of PL/SQL code for practice. Enter the code in Listing 7.5.

NOTE
Before you continue, make sure that at the SQL*Plus prompt you have entered SET SERVEROUTPUT ON. This allows you to see output to the screen as the PL/SQL code executes.

INPUT
Listing 7.5. Using TO_DATE within PL/SQL.

1: DECLARE
2:      v_Convert_Date DATE;
3: BEGIN
4:      v_Convert_Date := TO_DATE(`061167','MMDDYY');
5:      DBMS_OUTPUT.PUT_LINE(`The converted date is: ` || v_Convert_Date);
6: END;
7: /

Previous | Table of Contents | Next

Используются технологии uCoz