TO_CHAR()
Formats a date, number, or timestamp as a string using a format pattern.
Description
TO_CHAR converts a date, timestamp, or number into a formatted text
representation using a format pattern. It's the standard way to present dates
and numbers exactly how you want them — like YYYY-MM-DD or currency-style
output — without string concatenation.
Syntax
TO_CHAR(date, 'YYYY-MM-DD')
TO_CHAR(number, '999.99')Parameters
| Name | Description | Optional |
|---|---|---|
| value | The date/timestamp/number to format. | No |
| format | The format pattern controlling the output. | No |
Return Type
Returns a VARCHAR.
Examples
Employees & Departments
Loading database engine...
Employees & Departments
Loading database engine...
TO_CHAR vs CAST
CAST(x AS VARCHAR) gives a default format; TO_CHAR lets you control the
exact layout with a pattern.
Common Mistakes
- Format pattern dialect. Pattern tokens differ between engines (Postgres vs Oracle vs DuckDB); check your database's docs.
- Locale padding. Month names can be space-padded; use
FM(fill mode) in some engines to trim. - Number vs date patterns. The same function handles both, but the pattern must match the value's type.
Related Functions
See also: TO_DATE, CAST, EXTRACT.