// dates & times reference

SQL 12-hour vs 24-hour format

Every formatter has a 12-hour and a 24-hour code, and the letters differ by engine: HH24 vs HH12, %H vs %h, HH vs hh. Mixing them up shifts afternoon times by 12 hours.

Categoría
Dates & times
Returns
text
In this category
101 of 121

Sintaxis

TO_CHAR(d, 'HH24:MI')
DATE_FORMAT(d, '%h:%i %p')

Each block is a separate form. The support table and the examples show which engine takes which.

Soporte del motor

MotorSupportNotas
SQL Server Different name or syntax FORMAT(d, 'hh:mm tt'); no CONVERT style does 12-hour cleanly
PostgreSQL Sí Supported under this name.
MySQL Sí Supported under this name.
SQLite Sí %I and %p since 3.44; %H on any version
Oracle Sí Supported under this name.

Ejemplos

PostgreSQL

SELECT to_char(TIMESTAMP '2026-08-01 14:30', 'HH24:MI');  -- 14:30

MySQL

SELECT DATE_FORMAT('2026-08-01 14:30:00', '%h:%i %p');  -- 02:30 PM

SQL Server

SELECT FORMAT(CAST('2026-08-01 14:30' AS datetime2), 'hh:mm tt', 'en-US');  -- 02:30 PM

Conviene saberlo

  • Midnight and noon: 12-hour masks render midnight as 12, not 0; '%h:%i %p' at 00:30 gives 12:30 AM. Off-by-noon bugs come from mixing %h with %H.
  • AM/PM markers: SQL Server FORMAT 'tt' follows the culture argument, while MySQL's %p prints AM/PM under every lc_time_names setting, fr_FR included. PostgreSQL's AM/PM codes are not localized either.
  • PostgreSQL and Oracle: HH12 is the 12-hour clock and HH24 the 24-hour one. Plain HH is also 12-hour, so 'HH:MI' prints 14:30 as 02:30.

Questions about 12/24-hour

Why does my 'HH:MI' output show afternoon as 02?

Oracle and PostgreSQL's HH (and HH12) is the 12-hour clock. Use HH24, or add the AM/PM mask; HH alone hides whether 02 means morning.

Which engines default to 24-hour?

The default text forms are 24-hour on PostgreSQL, MySQL, SQLite and Oracle, as are CONVERT styles 120 and 126. SQL Server's style 0 (what CAST(datetime AS varchar) gives you) is 12-hour: 'Aug 1 2026 2:30PM'.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.