Menu

SQL Date Formatting by Database: MySQL, MariaDB, PostgreSQL & More

Compare date and time formatting in MySQL, MariaDB, PostgreSQL, SQL Server, SQLite, and Oracle, including the different format tokens.

Date and time formatting syntax is not portable across database engines. A format token that means month in one system can mean minutes in another. These examples format the same timestamp as 2026-10-02 13:04:05 in MySQL, MariaDB, PostgreSQL, SQL Server, SQLite, and Oracle.

Formatting functions return text for display or export. Keep values in date/time types for comparisons, sorting, and date arithmetic; don’t compare formatted strings as dates.

Quick reference

Database Function or expression Format pattern
MySQL / MariaDB DATE_FORMAT(value, format) %Y-%m-%d %H:%i:%s
PostgreSQL to_char(value, format) YYYY-MM-DD HH24:MI:SS
SQL Server CONVERT(char(19), value, 120) Style 120 returns yyyy-mm-dd hh:mi:ss in 24-hour time.
SQLite strftime(format, value) %Y-%m-%d %H:%M:%S
Oracle TO_CHAR(value, format) YYYY-MM-DD HH24:MI:SS

MySQL and MariaDB

Use DATE_FORMAT() with percent-prefixed specifiers. %m is the month; %i is the minute:

SELECT DATE_FORMAT('2026-10-02 13:04:05', '%Y-%m-%d %H:%i:%s') AS formatted;
formatted
---------------------
2026-10-02 13:04:05

MariaDB uses the same basic DATE_FORMAT() tokens. Locale-dependent names and other options can vary; see the MySQL 8.4 DATE_FORMAT() reference, MariaDB DATE_FORMAT() reference, and SQLiz’s MySQL DATE_FORMAT() guide.

PostgreSQL

Use to_char() with a template. PostgreSQL uses MM for month and MI for minute:

SELECT to_char(
    timestamp '2026-10-02 13:04:05',
    'YYYY-MM-DD HH24:MI:SS'
) AS formatted;
formatted
---------------------
2026-10-02 13:04:05

See PostgreSQL’s data type formatting functions and SQLiz’s to_char() reference.

SQL Server

For a fixed ISO-style output, CONVERT() accepts a style number. Style 120 formats a value as a 24-hour yyyy-mm-dd hh:mi:ss string:

SELECT CONVERT(char(19), CAST('2026-10-02T13:04:05' AS datetime2), 120) AS formatted;
formatted
-------------------
2026-10-02 13:04:05

See Microsoft’s CAST and CONVERT styles and SQLiz’s CONVERT() reference.

SQLite

Use strftime() with percent-prefixed substitutions. In SQLite, %M means minute, unlike MySQL/MariaDB where %M means a month name:

SELECT strftime('%Y-%m-%d %H:%M:%S', '2026-10-02 13:04:05') AS formatted;
formatted
-------------------
2026-10-02 13:04:05

See SQLite’s date and time functions and strftime() reference.

Oracle

Use TO_CHAR() with a date format model. Oracle and PostgreSQL both use MM for month and MI for minute in their common timestamp masks:

SELECT TO_CHAR(
    TIMESTAMP '2026-10-02 13:04:05',
    'YYYY-MM-DD HH24:MI:SS'
) AS formatted
FROM dual;
FORMATTED
-------------------
2026-10-02 13:04:05

See Oracle’s TO_CHAR datetime reference and SQLiz’s Oracle TO_CHAR() datetime guide.

Format token differences

Don’t copy format strings between engines without checking the tokens. For example:

Meaning MySQL / MariaDB PostgreSQL / Oracle SQLite
Month number %m MM %m
Minute %i MI %M
24-hour hour %H HH24 %H

SQL Server’s style-number form uses fixed layouts instead of per-token patterns. For more database-specific patterns and tokens, follow the reference links above.

Advertisement