Function Reference

TO_CHAR

MySQL equivalent:

Converts a date, time, interval or number to a string using a format template.

PostgreSQL 17.5
TO_CHAR(value, format)

Parameters

valuetimestamp | interval | numeric
Timestamp, interval or number
formattext
Format template, for example DD.MM.YYYY HH24:MI

Return value

A string (text)

Examples

Date and time in a custom format

PostgreSQL 17.5
SELECT TO_CHAR(
		TIMESTAMP '2024-03-08 14:30:00',
		'DD.MM.YYYY HH24:MI'
	);
to_char
08.03.2024 14:30

Day and month names without extra spaces

PostgreSQL 17.5
SELECT TO_CHAR(DATE '2024-03-08', 'FMDay, FMDD FMMonth YYYY');
to_char
Friday, 8 March 2024

Payment month from a table

PostgreSQL 17.5
SELECT date,
	TO_CHAR(date, 'YYYY-MM') AS MONTH
FROM Payments;
datemonth
2005-02-122005-02
2005-03-232005-03
2005-05-142005-05
2005-07-222005-07
2005-07-262005-07
2005-02-202005-02
2005-07-302005-07
2005-09-122005-09
2005-09-302005-09
2005-10-272005-10

Number with digit grouping

PostgreSQL 17.5
SELECT TO_CHAR(1234.5, 'FM999G999D00');
to_char
1,234.50

Details

Month and day names (Month, Day) are padded with spaces to 9 characters unless the pattern is preceded by FM.

See also