Chapter 4: SQL Language Elements
4.9.2.88 TO_CHAR function (extension)
Syntax
TO_CHAR ( expression [ , format_string ] )
Description
The scalar function TO_CHAR converts the given expression to character form and returns the result. The primary use for TO_CHAR is to format the output of date-time expressions through the format_string argument.
Arguments
expressionSpecifies the expression to be converted to character form. To use the format_string argument, expression must evaluate to a date or time value.
format_stringA date-time format string that specifies the format of the output. See “Date Format Strings” and “Time Format Strings” for details on format strings.
SQL ignores the format string if the expression argument does not evaluate to a date or time.
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
Notes
- The first argument to the function can be of any type.
- The second argument, if specified, must be of type CHARACTER.
- The result is of type CHARACTER.
- The format argument can be used only when the type of the first argument is DATE.
- If any of the argument expressions evaluates to null, the result is null.