Sub topic 11
Formatting and Parsing Text in PostgreSQL
Assume you know SELECT, WHERE, and basic PostgreSQL date and time types. This chapter focuses on converting between temporal values and text. Formatting is for display. Parsing is for turning controlled text input into a date or timestamp value.
Formatting values with `to_char`
to_char converts a date, timestamp, or number to text using a template. The template contains pattern codes.
| Pattern | Meaning | Example |
|---|---|---|
| `YYYY-MM-DD` | Four-digit year, month, and day | `2024-06-01` |
| `HH24:MI:SS` | 24-hour time, minute, and second | `13:30:45` |
| `TZH:TZM` | Numeric UTC offset | `-04:00` |
For a timestamptz, the displayed clock time and offset use the session's TimeZone setting.
SET TimeZone = 'America/New_York';
SELECT to_char(
TIMESTAMPTZ '2024-06-01 13:30:45.123+00',
'YYYY-MM-DD"T"HH24:MI:SS.MS TZH:TZM'
) AS formatted;
-- 2024-06-01T09:30:45.123 -04:00The T is enclosed in double quotes because it is literal text rather than a format code. MS prints milliseconds. Use to_char at the presentation boundary, such as a report or export. Do not use formatted text as the stored value or as the primary value for sorting and filtering.
Parsing with `to_timestamp` and `to_date`
to_timestamp(text, template) returns a timestamptz. If the input has no time zone, PostgreSQL interprets it in the current session time zone.
SET TimeZone = 'UTC';
SELECT to_timestamp(
'2024-06-01 09:30',
'YYYY-MM-DD HH24:MI'
) AS parsed;
-- 2024-06-01 09:30:00+00The session setting affects this parse. If the text represents a local time in Paris, parse it while the session uses Paris, or parse the text as a plain timestamp and apply the named zone explicitly at the application boundary.
to_date(text, template) returns a date and ignores time-of-day information.
SELECT to_date('01/06/2024', 'DD/MM/YYYY') AS parsed_date;
-- 2024-06-01Use MM for a month and MI for minutes. Confusing these codes is a common source of incorrect results. Use HH24 when input uses a 24-hour clock. HH expects a 12-hour clock and normally needs AM or PM.
Choosing a conversion path
For machine-readable input, prefer ISO 8601 literals or parameterized values over custom parsing. For example, TIMESTAMPTZ '2024-06-01 09:30:00+02' states its offset directly.
to_date and to_timestamp can accept some flexible or out-of-range input and may normalize it instead of rejecting it. Validate external input before saving it. A round-trip check can detect unexpected normalization:
WITH parsed AS (
SELECT to_date('2024-02-31', 'YYYY-MM-DD') AS d
)
SELECT d, to_char(d, 'YYYY-MM-DD') AS normalized
FROM parsed;
-- The normalized value reveals that the input was changed.Extracting fields
extract returns a numeric field from a temporal value. It is useful for reporting or calculations, while to_char is better for presentation.
SELECT extract(isodow FROM DATE '2024-06-01') AS weekday,
extract(epoch FROM TIMESTAMPTZ '2024-06-01 00:00:00+00') AS seconds;Remember that extracting fields from timestamptz uses the session time zone for calendar fields such as hour, day, and month.
Exercise
Which function should you use to produce 2024-06-01 09:30 for a report, and which function should you use to parse 2024/06/01 into a date?
Show answer
Use to_char for the report text and to_date('2024/06/01', 'YYYY/MM/DD') for the date.