Format a date or timestamp value as a string in a specific display format. Every engine has a different function name and format code system.
Formats a date or timestamp into a string using format specifiers. MySQL uses DATE_FORMAT(), SQLite uses STRFTIME(), SQL Server uses FORMAT(). Useful for generating human-readable date strings in queries.
| Database System | Support Status | Since Version | Notes |
|---|---|---|---|
| MySQL | ✓ Supported | all | DATE_FORMAT(date, format) uses % format codes: %Y (4-digit year), %m (month 01-12), %d (day 01-31), %H (hour 00-23), %i (minute), %s (second). TIME_FORMAT() for time-only values. |
| PostgreSQL | ✓ Supported | all | TO_CHAR(timestamp, format) uses its own format codes: YYYY (year), MM (month), DD (day), HH24 (hour), MI (minute), SS (second). TO_DATE() and TO_TIMESTAMP() for parsing. Very rich format string options including ordinals (DDth), spelled-out months (Month), etc. |
| SQL Server | ✓ Supported | 2012 | FORMAT(value, format [, culture]) uses .NET format strings. Flexible but slow on large datasets — avoid in WHERE clauses or high-volume queries. Pre-2012: use CONVERT(VARCHAR, date, style) with numeric style codes (103 = dd/mm/yyyy, 120 = yyyy-mm-dd hh:mi:ss). |
| Oracle | ✓ Supported | all | TO_CHAR(date, format) using Oracle format codes: YYYY, MM, DD, HH24, MI, SS. TO_DATE(str, format) for parsing. Format codes differ from PostgreSQL's despite the same function name — Oracle uses HH24 for 24h hour, PostgreSQL also uses HH24, so that one is compatible. |
| SQLite | ✓ Supported | all | strftime(format, date) uses % format codes similar to C's strftime: %Y, %m, %d, %H, %M, %S. date() and time() return fixed ISO 8601 formats. No TO_CHAR or DATE_FORMAT. |
Date formatting is almost entirely non-portable. MySQL uses % codes (DATE_FORMAT), PostgreSQL and Oracle both use TO_CHAR but with slightly different code sets, SQL Server's FORMAT() uses .NET strings, and SQLite uses C-style strftime. The only safe portable path is formatting dates in application code rather than SQL.
DATE_FORMAT(date, format) uses % format codes: %Y (4-digit year), %m (month 01-12), %d (day 01-31), %H (hour 00-23), %i (minute), %s (second). TIME_FORMAT() for time-only values.
TO_CHAR(timestamp, format) uses its own format codes: YYYY (year), MM (month), DD (day), HH24 (hour), MI (minute), SS (second). TO_DATE() and TO_TIMESTAMP() for parsing. Very rich format string options including ordinals (DDth), spelled-out months (Month), etc.
FORMAT(value, format [, culture]) uses .NET format strings. Flexible but slow on large datasets — avoid in WHERE clauses or high-volume queries. Pre-2012: use CONVERT(VARCHAR, date, style) with numeric style codes (103 = dd/mm/yyyy, 120 = yyyy-mm-dd hh:mi:ss).
TO_CHAR(date, format) using Oracle format codes: YYYY, MM, DD, HH24, MI, SS. TO_DATE(str, format) for parsing. Format codes differ from PostgreSQL's despite the same function name — Oracle uses HH24 for 24h hour, PostgreSQL also uses HH24, so that one is compatible.
strftime(format, date) uses % format codes similar to C's strftime: %Y, %m, %d, %H, %M, %S. date() and time() return fixed ISO 8601 formats. No TO_CHAR or DATE_FORMAT.