Dates and times

TO_CHAR

Converts a date, time or number to text in the format you need.

Syntax

TO_CHAR(<value>, '<format>')

The format is made of patterns: DD — day, MM — month, YYYY — year, HH24 — hour, MI — minutes. Everything else, such as dots and colons, stays as it is. You need it when a date has to be shown to people in a familiar form rather than the way the database stores it.

Data for the examples

orders

idcreated_atamount
12026-07-03 10:15:00120.50
22026-07-28 18:40:0080.25
32026-08-09 09:05:00199.99
42026-08-21 14:30:0045.00
52026-09-02 11:00:0064.75

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

A day-first date and 24-hour time
SELECT TO_CHAR(TIMESTAMP '2026-09-13 15:47', 'DD.MM.YYYY') AS formatted_date,
TO_CHAR(TIMESTAMP '2026-09-13 15:47', 'HH24:MI') AS formatted_time;
formatted_dateformatted_time
13.09.202615:47
The date of each order
SELECT id,
TO_CHAR(created_at, 'DD.MM.YYYY HH24:MI') AS created
FROM orders;
idcreated
103.07.2026 10:15
228.07.2026 18:40
309.08.2026 09:05
421.08.2026 14:30
502.09.2026 11:00
The name of the month
SELECT id,
TO_CHAR(created_at, 'FMMonth YYYY') AS month
FROM orders;
idmonth
1July 2026
2July 2026
3August 2026
4August 2026
5September 2026

FM removes the spaces PostgreSQL uses to pad month names to the same length.

Watch out

MM is the month; minutes are MI. TO_CHAR(TIMESTAMP '2026-09-13 15:47', 'HH24:MM') shows 15:09: the hour and the month number.

The result is text, so it sorts as text: August, July, September. Sort by the date itself and keep TO_CHAR for display.

Without FM, month names are padded with spaces to 9 characters: the 'Month' format for July gives July followed by five spaces.

Related functions