Dates in oracle are stored in a format that doesn’t always make them friendly from a reporting point of view or dealing with data in Excel point of view, the TO_CHAR function can reformat your datetime fields so that they are more user friendly.

Examples

The statement is written with two parameters the exact name of the date field and the format (see second table).

TO_CHAR(DATEFIELD,’FORMAT’)

StatementResult
TO_CHAR(DATEFIELD,’dd mm year’)23 05 twenty thirteen
TO_CHAR(DATEFIELD,’dd/mm/yy’)23/05/2013
TO_CHAR(DATEFIELD,’Day Month Year’)23 May Twenty Thirteen
TO_CHAR(DATEFIELD,’dd/mm/yyyy hh24:mi’)23/05/2013 18:38

Example formats

Any combination of the following formats can be used, although obviously certain combinations may not make any sense to your end users.

DescriptionExample
Year/yearYear spelt out in text (with and without an upper case first digitTwenty Thirteen / twenty thirteen
yyyyYear number in full2008
yyLast two digits of year number08
qQuarter of the year1 (February)
mmMonth number within year**12 (December)
Mon / monAbbreviated month name (with and without an uppercase first digit)Oct/oct
Month / monthMonth name (with and without an upper case first digit)October / october
wWeek number* (within month)1 (01/05/2013)
wwWeek number* (within year) **18 (01/05/2013)
dDay of the week1 (Monday)
ddDay number within month**
Dy/dyAbbreviated day name (with and without an uppercase first digit)Mon / mon
Day / dayDay name (with and without an upper case first digit)Monday / Monday
hh24Hour of the day in 24 hour format**16 (4pm)
hhHour of the day in 12 hour format**04 (4pm)
miMinute of the hour**52 (16:52)

*Important note about week numbers

Week numbers in oracle can be confusing as they start on the first day of the year and count seven days and then change, for example in 2013 the week number would increase each Wednesday.

**Suppression of zeros

This format can be prefixed with FM to suppress zeros, for example mm would display January as “01” where as if FMmm was used it would show “1”.

How to test you date formatting

The easiest method for testing the reformatting of your dates is to test them using the dual function, run the following SQL statement and substitute the area highlighted in yellow with the date format you are trying to use.

select to_char(to_date(’01/05/2013 16:30′,’dd/mm/yyyy hh24:mi’),’Year’) from dua

Posted in

Leave a comment