Monday, January 19, 2009

Using TO_CHAR function with Dates

TO_CHAR(date,'format_model')

format_model must be enclosed within quotation marks and had an 'fm' element to remove padded blanks or suppress leading zeros.

Format model can be any of the following:
YYYY: Full year in numbers
YEAR: Year spelled out in English
MM: Two-digit value for month
MONTH: Full name of the month
MON: Three-letter abbreviation of the month
DY: Three-letter abbreviation of day of the week
DAY: Full name of the day of week
DD: Numeric day of the month

Examples using date format model:
SQL> select to_char(sysdate, 'ddspth MON YYYY') from dual;
TO_CHAR(SYSDATE,'DDSPTH
-----------------------
nineteenth JAN 2009

SQL> select to_char(sysdate, 'dd month year') from dual;
TO_CHAR(SYSDATE,'DDMONTHYEAR')
-------------------------------------------------------
19 january two thousand nine

SQL> select to_char(to_date('01-JAN-2009'), 'dd month year') from dual;
TO_CHAR(TO_DATE('01-JAN-2009')
------------------------------
01 january two thousand nine

SQL> select to_char(to_date('01-JAN-2009'), 'fmdd month year') from dual;
TO_CHAR(TO_DATE('01-JAN-200
---------------------------
1 january two thousand nine

No comments:

Post a Comment