The TO_CHAR scalar function supports a variety of format strings to control the output of date and time values. The format strings consist of keywords that FairCom DB SQL interprets and replaces with formatted values.
The format strings are case sensitive. For instance, FairCom DB SQL replaces ‘DAY’ with all uppercase letters, but follows the case of ‘Day’.
Supply the format strings, enclosed in single quotation marks, as the second argument to the TO_CHAR function.
Example
SELECT C1 FROM T2;
C1
--
09/29/1952
1 record selected
SELECT TO_CHAR(C1, 'Day, Month ddth'),
TO_CHAR(C2, 'HH12 a.m.') FROM T2;
TO_CHAR(C1,DAY, MONTH DDTH) TO_CHAR(C2,HH12 A.M.)
--------------------------- ---------------------
Monday , September 29th 02 p.m.
1 record selected
For details of the TO_CHAR function, see "TO_CHAR".
Date Format Strings
A date format string can contain any of the following format keywords along with other characters. The format keywords in the format string are replaced by corresponding values to get the result. The other characters are displayed as literals.
CC |
The century as a two digit number. |
YYYY |
The year as a four digit number. |
YYY |
The last three digits of the year. |
YY |
The last two digits of the year. |
Y |
The last digit of the year. |
Y,YYY |
The year as a four digit number with a comma after the first digit. |
Q |
The quarter of the year as one digit number (with values one, two, three, or four). |
MM |
The month value as two digit number (in the range 01-12). |
MONTH |
The name of the month as a string of nine characters (‘JANUARY’ to ‘DECEMBER’). |
MON |
The first three characters of the name of the month (in the range ‘JAN’ to ‘DEC’). |
WW |
The week of year as a two digit number (in the range 01-52). |
W |
The week of month as a one digit number (in the range 1-5). |
DDD |
The day of year as a three digit number (in the range 001-365). |
DD |
The day of month as a two digit number (in the range 01-31). |
D |
The day of week as a one digit number (in the range 1-7, 1 for Sunday and 7 for Saturday). |
DAY |
The day of week as a nine character string (in the range ‘SUNDAY’ to ‘SATURDAY’. |
DY |
The day of week as a three character string (in the range ‘SUN’ to ‘SAT’). |
J |
The Julian day (number of days since DEC 31, 1899) as an eight digit number. |
TH |
When added to a format keyword that results in a number, this format keyword (‘TH’) is replaced by the string ‘ST’, ‘ND’, ‘RD’ or ‘TH’ depending on the last digit of the number. |
Example
SELECT C1 FROM T2;
C1
--
09/29/1952
1 record selected
SELECT TO_CHAR(C1, 'Day, Month ddth'),
TO_CHAR(C2, 'HH12 a.m.') FROM T2;
TO_CHAR(C1,DAY, MONTH DDTH) TO_CHAR(C2,HH12 A.M.)
--------------------------- ---------------------
Monday , September 29th 02 p.m.
1 record selected
Time Format Strings
A time format string can contain any of the following format keywords along with other characters. The format keywords in the format string are replaced by corresponding values to get the result. The other characters are displayed as literals.
AM |
The string "AM" or "PM" depending on whether time corresponds to forenoon or afternoon. |
PM |
The string "AM" or "PM" depending on whether time corresponds to forenoon or afternoon. |
A.M. |
The string "A.M." or "P.M." depending on whether time corresponds to forenoon or afternoon. |
P.M. |
The string "A.M." or "P.M." depending on whether time corresponds to forenoon or afternoon. |
HH12 |
The hour value as a two-digit number (in the range 00 to 11). |
HH24 |
The hour value as a two-digit number (in the range 00 to 23). |
HH |
The hour value as a two-digit number (in the range 00 to 23). |
MI |
The minute value as a two-digit number (in the range 00 to 59). |
SS |
The seconds value as a two-digit number (in the range 00 to 59). |
SSSSS |
The seconds from midnight as a five-digit number (in the range 00000 to 86399). |
Example
SELECT C1 FROM T2;
C1
--
09/29/1952
1 record selected
SELECT TO_CHAR(C1, 'Day, Month ddth'),
TO_CHAR(C2, 'HH12 a.m.') FROM T2;
TO_CHAR(C1,DAY, MONTH DDTH) TO_CHAR(C2,HH12 A.M.)
--------------------------- ---------------------
Monday , September 29th 02 p.m.
1 record selected