Home » Oracle SQL » ORACLE TO CHAR

ORACLE TO CHAR

The Oracle TO_CHAR function is used to convert a DATE value to string or text value.

The TO_CHAR function is very useful for formatting the dates in a table to a specified date format.

Syntax

The syntax of TO_CHAR function is as follows:

TO_CHAR( date, format )

Here,

Date – It is a DATE value in a table. The data type of this value can be DATE, INTERVAL, TIMESTAMP, TIMESTAMP WITH TIME ZONE, or TIMESTAMP WITH LOCAL TIME ZONE.

Format – It is the date format in which we want our string to be. When we convert a date value to string, we can convert into different formats as per our requirement.

The TO_CHAR function returns the string in a desired date format like DD-MM-YYYY or MMM-DD-YYYY etc.

Examples

1. TO_CHAR with Numbers and Decimals

TO_CHAR function can be used with not only dates but also numbers and decimals to covert them in desired format. Lets have a look at few queries:

TO_CHAR(3150.48, '9999.9')
Result: ' 3150.4'

TO_CHAR(-3150.48, '9999.9')
Result: '-3150.4'

TO_CHAR(3150.48, '$9,999.00')
Result: ' $3150.48'

TO_CHAR(3150.48, '9,999.99')
Result: ' 3,150.48'

TO_CHAR(48, '0099')
Result: ' 0048'

2. TO_CHAR with SYSDATE

We can use TO_CHAR function to convert SYSDATE into desired date string format.

SELECT
TO_CHAR( sysdate, 'MM-DD-YYYY' )
FROM
dual;

Result:

Oracle TO CHAR function with SYS date

Similarly, we can use TO_CHAR function to convert the date value into long date format. Lets see the query:

SELECT
TO_CHAR( sysdate, 'MON DDth, YYYY' )
FROM
dual;

Result:

TO CHAR to convert date to other format

We can use any date format from the table below to convert our query into desired date formats.

FormatDescription
YEARYear, spelled out
YYYY4-digit year
YYY YY YLast 3, 2, or 1 digit(s) of year.
IYY IY ILast 3, 2, or 1 digit(s) of ISO year.
IYYY4-digit year based on the ISO standard
QQuarter of year (1, 2, 3, 4; JAN-MAR = 1).
MMMonth (01-12; JAN = 01).
MONAbbreviated name of month.
MONTHName of month, padded with blanks to length of 9 characters.
WWWeek of year (1-53) where week 1 starts on the first day of the year
and continues to the seventh day of the year.
WWeek of month (1-5) where week 1 starts on the first day
of the month and ends on the seventh.
DDay of week (1-7).
DAYName of day.
DDDay of month (1-31).
DDDDay of year (1-366).
HHHour of day (1-12).
HH12Hour of day (1-12).
HH24Hour of day (0-23).
MIMinute (0-59).
SSSeconds (0-59).

3. TO_CHAR for formatting Date values in a column

TO_CHAR function can be used for formatting date values in a column and converting them in any Oracle recognized date format.

Lets see a query to understand this better:

SELECT
first_name,
last_name,
hire_date,
TO_CHAR( hire_date, 'dd-mm-yyyy' ) format_1,
TO_CHAR( hire_date, 'ddth-mm-yyyy' ) format_2
FROM
employees;

Result:

TO CHAR Oracle Example 3

The query converts the date values in hire_date column into 2 string formats. We can use any format from the Oracle recognized date formats for conversion.

In this article, we saw how to use TO_CHAR function to convert date values into string.