SQL FORMAT date function
Formats a date with .NET custom format strings and optional culture: the closest SQL Server gets to TO_CHAR's expressiveness.
- Категория
- Dates & times
- Returns
nvarchar- In this category
- 95 of 121
Синтаксис
FORMAT(date, 'format' [, culture])
Поддержка движка
| Движок | Support | Заметки |
|---|---|---|
| SQL Server | Да | Supported under this name. |
| PostgreSQL | Different name or syntax | to_char(d, 'YYYY-MM-DD'); masks, not .NET codes |
| MySQL | Different name or syntax | DATE_FORMAT(d, '%Y-%m-%d') |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Different name or syntax | TO_CHAR(d, 'YYYY-MM-DD', 'NLS_DATE_LANGUAGE=...') |
Примеры
SQL Server
SELECT FORMAT(GETDATE(), 'yyyy-MM-dd');
SQL Server
SELECT FORMAT(CAST('2026-08-01' AS date), 'dddd, dd MMMM yyyy', 'de-DE'); -- Samstag, 01 August 2026
PostgreSQL
SELECT to_char(DATE '2026-08-01', 'FMDay, DD FMMonth YYYY'); -- Saturday, 01 August 2026
Полезно знать
- FORMAT calls into the .NET runtime for every row and is far slower than CONVERT; on large result sets, use CONVERT with a style number.
- Codes are .NET custom strings: 'yyyy-MM-dd HH:mm:ss'. Capital M is month and lowercase m is minute; HH is 24-hour and hh is 12-hour.
- The culture argument changes names and order: FORMAT(d, 'D', 'de-DE') gives German long dates, something TO_CHAR needs NLS parameters for.
Questions about FORMAT
Is FORMAT slow enough to matter?
Yes on sets: benchmarks routinely show it 10x+ slower than CONVERT for the same output. Use it for one-off presentation, not for GROUP BY or large exports.
What is the difference between 'MM' and 'mm'?
In .NET masks, 'MM' is two-digit month, 'mm' is minutes (the reverse intuition of many C formats). 'dd' is day, 'dddd' the day name.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- 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
- Grant SELECT on all tables Snippet
- List materialized views and refresh times Snippet
- Move a table between schemas Snippet
- Pad numbers with leading zeros in SQL Snippet
Cheat sheets for the engines that have it: SQL Server, PostgreSQL, MySQL, 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.