SQL TO_CHAR / date formatting function
Renders a timestamp as text in a template: 'YYYY-MM-DD', month names, 12-hour clocks. Every engine can do it, but the function names and format codes differ.
- Categoria
- Dates & times
- Returns
string- In this category
- 8 of 121
Sintaxe
TO_CHAR(ts, 'YYYY-MM-DD')
DATE_FORMAT(ts, '%Y-%m-%d')
strftime('%Y-%m-%d', ts)
FORMAT(ts, 'yyyy-MM-dd')
Each block is a separate form. The support table and the examples show which engine takes which.
Suporte a mecanismos
| Motor | Support | Notas |
|---|---|---|
| SQL Server | Different name or syntax | no TO_CHAR; FORMAT(d, 'yyyy-MM-dd') or CONVERT with style codes |
| PostgreSQL | Sim | Supported under this name. |
| MySQL | Different name or syntax | DATE_FORMAT(ts, '%Y-%m-%d') |
| SQLite | Different name or syntax | strftime('%Y-%m-%d', ts): format first, then the value |
| Oracle | Sim | Supported under this name. |
Exemplos
PostgreSQL
SELECT TO_CHAR(created_at, 'FMDay, DD Month YYYY') AS pretty FROM orders;
MySQL
SELECT DATE_FORMAT(created_at, '%W, %d %M %Y') AS pretty FROM orders;
Oracle
SELECT TO_CHAR(created_at, 'FMDay, DD Month YYYY') AS pretty FROM orders;
SQLite
SELECT strftime('%Y-%m-%d %H:%M', created_at) AS stamp FROM orders;
Bom saber
- The codes are per-engine: PostgreSQL/Oracle 'YYYY-MM-DD HH24:MI', MySQL '%Y-%m-%d %H:%i', SQLite '%Y-%m-%d %H:%M', SQL Server FORMAT 'yyyy-MM-dd HH:mm'. A format string has to be rewritten for each engine.
- TO_CHAR pads month and day names to a fixed width: 'Month' gives 'May '. The FM prefix ('FMMonth') removes the padding on PostgreSQL and Oracle.
- SQL Server's FORMAT is convenient but CLR-backed and slow on large sets; CONVERT with style numbers is the fast path.
- Formatting is for display. Storing formatted dates in columns destroys sorting, arithmetic and indexes. Keep the native type.
Questions about TO_CHAR / DATE_FORMAT
What are the format codes for a date with time?
PostgreSQL/Oracle: 'YYYY-MM-DD HH24:MI:SS'. MySQL: '%Y-%m-%d %H:%i:%s'. SQLite: '%Y-%m-%d %H:%M:%S'. SQL Server: CONVERT(varchar, d, 120) with style 120.
Why is FORMAT so slow in SQL Server?
SQL Server has no TO_CHAR, and its FORMAT function calls into the .NET CLR once per row. CONVERT(varchar(30), d, style) is native and much faster on large result sets.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Format a date as text in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Get the first day of the month in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Convert a number to formatted text in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Format money and currency values as strings in SQL Snippet
- Get the ISO week number of a date in SQL Snippet
- Round timestamps to the nearest minute or hour in SQL Snippet
Cheat sheets for the engines that have it: PostgreSQL, MySQL, Oracle, SQL Server, SQLite.
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.