SQL ISO 8601 date format
ISO 8601 output is the interchange-safe shape for timestamps: unambiguous, sortable, and one quoted mask character away on each engine.
- Category
- Dates & times
- Returns
text- In this category
- 98 of 121
Syntax
TO_CHAR(d, 'YYYY-MM-DD"T"HH24:MI:SS')
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Yes | CONVERT(varchar, d, 126): ISO 8601 natively |
| PostgreSQL | Yes | to_char(d, 'YYYY-MM-DD"T"HH24:MI:SS') |
| MySQL | Yes | DATE_FORMAT(d, '%Y-%m-%dT%H:%i:%s') |
| SQLite | Yes | strftime('%Y-%m-%dT%H:%M:%S', d) |
| Oracle | Yes | TO_CHAR(d, 'YYYY-MM-DD"T"HH24:MI:SS') |
Examples
SQL Server
SELECT CONVERT(varchar(30), CAST('2026-08-01 14:30:00 +02:00' AS datetimeoffset), 127); -- 2026-08-01T12:30:00Z
PostgreSQL
SELECT to_char(now() AT TIME ZONE 'UTC', 'YYYY-MM-DD"T"HH24:MI:SS"Z"');
SQLite
SELECT strftime('%Y-%m-%dT%H:%M:%SZ', 'now');
Good to know
- Literal characters inside format masks need quoting: the T in PostgreSQL and Oracle is "T" (double quotes), MySQL and SQLite splice it straight in.
- Offsets are engine-specific: SQL Server style 126 on a datetimeoffset keeps its offset (+02:00) and style 127 converts to UTC with a trailing Z; PostgreSQL's to_char has OF (+02) and TZH:TZM; MySQL has no offset code, so append it yourself.
- MySQL's STR_TO_DATE parses the shape back with '%Y-%m-%dT%H:%i:%s', and SQL Server's style 126 parses it in CONVERT; keep output and parse styles symmetric.
Questions about ISO 8601 output
Should I store ISO strings instead of timestamps?
No. Store a real timestamp type and format on the way out. ISO text storage loses range checks, arithmetic and index-ordered time scans.
Why does my 'T' vanish or error?
In Oracle and PostgreSQL masks, unquoted letters are format codes. Quote them: 'YYYY-MM-DD"T"HH24:MI:SS'.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, SQLite, Oracle.
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.