SQL STRFTIME function
Formats an SQLite date or time value as a string using C-style % placeholders.
- 類別
- Strings
- Returns
string- In this category
- 110 of 172
語法
strftime(format, time_value [, modifier, ...])
引擎支援
| 引擎 | Support | 備註 |
|---|---|---|
| SQL Server | Different name or syntax | Use FORMAT(date, format) or CONVERT with a style. |
| PostgreSQL | Different name or syntax | Use TO_CHAR(timestamp, format). |
| MySQL | Different name or syntax | Use DATE_FORMAT(date, format). |
| SQLite | 是 | Supported under this name. |
| Oracle | Different name or syntax | Use TO_CHAR(date, format). |
範例
SQLite
SELECT strftime('%Y-%m-%d', 'now') AS today;
SQLite
SELECT strftime('%s', created_at) AS epoch
FROM events;
SQLite
SELECT strftime('%Y-%m-%d', created_at, 'start of month') AS month
FROM orders;
小知識
- Accepted inputs: 'YYYY-MM-DD', 'YYYY-MM-DD HH:MM[:SS[.SSS]]' with a space or a T, time-only 'HH:MM' forms, an optional timezone suffix, 'now', or a number (a Julian day, or Unix seconds with the 'unixepoch' modifier). Anything else, such as '20/11/2026', returns NULL.
- Common placeholders: %Y %m %d %H %M %S, %s (Unix seconds), %j (day of year), %w (weekday, 0 = Sunday), %W (week of year, weeks starting Monday).
- Modifiers apply left to right: strftime('%Y-%m-%d', 'now', '-1 day', 'start of month') takes yesterday, then the first of that month.
Questions about STRFTIME
How do I get a Unix timestamp in SQLite?
strftime('%s', value) returns seconds since 1970 as text. unixepoch(value), added in SQLite 3.38, returns it as an integer.
What is the MySQL equivalent of strftime?
DATE_FORMAT(date, '%Y-%m-%d') uses the same % idea, but the letters differ: minutes are %i in MySQL and %M in SQLite, where MySQL's %M is the month name.
Why does strftime return NULL on my datetime string?
The input is not in a format SQLite recognises. ISO forms with a space or T work; '20/11/2026' or 'Nov 20 2026' do not. Store dates as ISO-8601 text, Julian days or Unix seconds.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
- Current timestamp and its precision in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Format a date as text in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Get the Unix timestamp of a date in SQL Server, PostgreSQL, MySQL, SQLite and Oracle Dialect comparison
- Group rows into hourly buckets Snippet
- Audit soft-deleted rows Snippet
Cheat sheets for the engines that have it: SQLite, 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.