to_char
The to_char function formats various data types, including date/time, integer, float point and numeric into a formatted string.
Syntax
The syntax for this function is:
TO_CHAR(timestamp, format_string)
TO_CHAR(interval, format_string)
Arguments
-
timestamp:TIMESTAMPorTIMESTAMP WITH TIMEZONEvalue to format. -
format: Format of the output string.
If the format string is NULL, to_char returns an empty string ('') instead of NULL. This behavior is compatible with PostgreSQL.
|
Supported formats
The string format supports these template patterns (case-insensitive):
| Pattern | Description |
|---|---|
|
Year (1-9999) |
|
Month number (01–12) |
|
Day of month (01–31) |
|
Hour of day (1–12) |
|
Hour of day (1–12) |
|
Hour of day (0–23) |
|
Minute (0–59) |
|
Second (0–59) |
|
Millisecond (0–999) |
|
Microsecond (0–999999) |
|
Meridiem indicator without periods |
|
Meridiem indicator with periods |
General restrictions
-
All text inside double quote
"{text}"will not be considered a pattern -
The quote character (
") will not appear in the result string -
Any text that is not a template pattern is copied verbatim (for example, preserved in the result string)
Interval overflow restrictions
Interval overflow occurs when an operation involving interval values exceeds the maximum limits of the interval data type, resulting in an error or unexpected behavior. This can happen when adding, subtracting or multiplying interval values that lead to a representation that goes beyond the allowable range for any of its components (for example, years, months, days, hours, minutes and seconds). When executing the to_char function for intervals, it is important to be aware of these overflow restrictions:
| Conversion | Source Component | Target Component |
|---|---|---|
Days to Months |
Days |
Months |
Hours to Days |
Hours |
Days |
Seconds to Days |
Seconds |
Days |
All in all, for intervals the date overflow doesn’t apply (units smaller than an hour can only overflow into hours, but not into days and so on), any excess units will not carry over to the next larger unit.
Examples
Intervals
This query converts an interval and displays it in a specified string format:
SELECT TO_CHAR('25 months'::INTERVAL,'"YEAR:" YYYY "MONTH:" MM') AS FORMATTED_INTERVAL;
SELECT TO_CHAR('13 days' + '49 hours'::INTERVAL, '"Day:" DD "Hour:" HH') AS FORMATTED_INTERVAL;
SELECT TO_CHAR('65 seconds'::INTERVAL, '"MINUTE": MI "SECOND": SS') AS FORMATTED_INTERVAL;
Outputs:
FORMATTED_INTERVAL
---------------------------------------
YEAR: 0002 MONTH: 01
FORMATTED_INTERVAL
---------------------------------------
Day: 13 Hour: 01
FORMATTED_INTERVAL
---------------------------------------
MINUTE: 01 SECOND: 05
Timestamps
This query retrieves the current timestamp and displays it in a specified string format:
SELECT TO_CHAR(CURRENT_TIMESTAMP(), '"YEAR:" YYYY "MONTH:" MM "DAY:" DD') AS FORMATTED_TIMESTAMP;
SELECT TO_CHAR(CURRENT_TIMESTAMP(), 'YYYY-MM-DD HH24:MI:SS.US') AS FORMATTED_TIMESTAMP;
SELECT TO_CHAR(CURRENT_TIMESTAMP(), 'YYYY-MM-DD HH12:MI:SS a.m.') AS FORMATTED_TIMESTAMP;
Outputs:
FORMATTED_TIMESTAMP
---------------------------------------
YEAR:2025 MONTH:01 DAY:01
FORMATTED_TIMESTAMP
---------------------------------------
2025-01-01 08:08:03.001200
FORMATTED_TIMESTAMP
---------------------------------------
2025-01-01 08:08:03 p.m.