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'
);Day and month names without extra spaces
PostgreSQL 17.5
SELECT TO_CHAR(DATE '2024-03-08', 'FMDay, FMDD FMMonth YYYY');Payment month from a table
PostgreSQL 17.5
SELECT date,
TO_CHAR(date, 'YYYY-MM') AS MONTH
FROM Payments;Number with digit grouping
PostgreSQL 17.5
SELECT TO_CHAR(1234.5, 'FM999G999D00');Details
Month and day names (Month, Day) are padded with spaces to 9 characters unless the pattern is preceded by FM.