Contents
to_char()
A function for formatting dates, timestamps and numeric types
to_char()
is a system function for outputting dates, timestamps and numeric values in a specified format.
to_char()
was added in PostgreSQL 7.0.
Usage
to_char (timestamp
,text
) →text
to_char (timestamp with time zone
,text
) →text
to_char (interval
,text
) →text
to_char (numeric_type
,text
) →text
to_char()
converts the provided value into the specified template format; see the PostgreSQL documentation section Template Patterns for Date/Time Formatting for details.
Change history
Work-in-progress
- PostgreSQL 15
- support for
of
,tzh
andtzm
format patterns added (commit 9dde8289)
- support for
- PostgreSQL 11
TZH
andTZM
format patterns added for specifying the time zone's offset fromUTC
in hours and minutes (commit 11b623dd)
- PostgreSQL 9.4
- timezone offset format specifier
OF
added to output the timezone's UTC offset (commit 7408c5d2)
- timezone offset format specifier
- PostgreSQL 7.2
to_char(interval, text)
version added (commit 2a34134b)
- PostgreSQL 7.0
- added (commit b866d2e2)
Examples
Basic example for formatting a timestamp using to_char()
:
postgres=# SELECT to_char(now(), 'YYYY-MM-DD HH24:MI'); to_char ------------------ 2021-08-01 10:02 (1 row)
References
- PostgreSQL documentation: Formatting Functions
- PostgreSQL documentation: Template Patterns for Date/Time Formatting