SQL function
SQLite strftime(): format dates and times
Updated · Tested on PostgreSQL 18.6 and SQLite 3.54.1 (results generated , re-checked on every release)
Short answer
strftime(format, date, modifiers…) turns a date or time into text in the format you give: strftime('%Y-%m', '2026-03-15') returns '2026-03'. It's SQLite's date formatter; PostgreSQL has no strftime and uses to_char and EXTRACT instead.
Syntax and availability
| PostgreSQL | SQLite | |
|---|---|---|
| Available | No: use to_char() or EXTRACT() | Yes, built in |
| Syntax | to_char(date_or_timestamp, 'YYYY-MM') -- the PostgreSQL equivalent | strftime(format, time_value [, modifier, …]) |
Examples
Year and month of each arrival
PostgreSQL
SELECT name, arrived_on,
to_char(arrived_on, 'YYYY') AS year,
to_char(arrived_on, 'MM') AS month
FROM pets
ORDER BY arrived_on
LIMIT 5;SQLite
SELECT name, arrived_on,
strftime('%Y', arrived_on) AS year,
strftime('%m', arrived_on) AS month
FROM pets
ORDER BY arrived_on
LIMIT 5;Result (PostgreSQL and SQLite, identical): 5 rows
| name | arrived_on | year | month |
|---|---|---|---|
| Tofu | 2025-04-04 | 2025 | 04 |
| Gizmo | 2025-05-23 | 2025 | 05 |
| Bruno | 2025-06-30 | 2025 | 06 |
| Rocket | 2025-07-09 | 2025 | 07 |
| Socks | 2025-08-17 | 2025 | 08 |
Try it: PostgreSQL playground · SQLite playground
Orders per weekday (0 = Sunday)
PostgreSQL
SELECT EXTRACT(DOW FROM ordered_on)::text AS weekday, COUNT(*) AS orders
FROM orders
GROUP BY weekday
ORDER BY weekday;SQLite
SELECT strftime('%w', ordered_on) AS weekday, COUNT(*) AS orders
FROM orders
GROUP BY weekday
ORDER BY weekday;Result (PostgreSQL and SQLite, identical): 6 rows
| weekday | orders |
|---|---|
| 0 | 6 |
| 2 | 2 |
| 3 | 3 |
| 4 | 3 |
| 5 | 1 |
| 6 | 2 |
Try it: PostgreSQL playground · SQLite playground
Sign-ups per month
PostgreSQL
SELECT to_char(signed_up_on, 'YYYY-MM') AS month, COUNT(*) AS signups
FROM users
GROUP BY month
ORDER BY month;SQLite
SELECT strftime('%Y-%m', signed_up_on) AS month, COUNT(*) AS signups
FROM users
GROUP BY month
ORDER BY month;Result (PostgreSQL and SQLite, identical): 3 rows
| month | signups |
|---|---|
| 2026-01 | 80 |
| 2026-02 | 100 |
| 2026-03 | 120 |
Try it: PostgreSQL playground · SQLite playground
How it behaves: edge cases, measured
Each expression was run as SELECT … on PostgreSQL 18.6 and SQLite 3.54.1 when this page was built. Nothing here is typed by hand.
| Case | PostgreSQL | SQLite |
|---|---|---|
| Year | to_char(DATE '2026-03-15', 'YYYY')'2026' | strftime('%Y', '2026-03-15')'2026' |
| Month, zero-padded | to_char(DATE '2026-03-05', 'MM')'03' | strftime('%m', '2026-03-05')'03' |
| Day of week (0 = Sunday) | EXTRACT(DOW FROM DATE '2026-03-15')0 | strftime('%w', '2026-03-15')'0' |
| Day of the year | to_char(DATE '2026-03-15', 'DDD')'074' | strftime('%j', '2026-03-15')'074' |
| Week number (they count weeks differently) | to_char(DATE '2026-01-01', 'IW')'01' | strftime('%W', '2026-01-01')'00' |
| Hours and minutes of a timestamp | to_char(TIMESTAMP '2026-03-15 14:05:09', 'HH24:MI')'14:05' | strftime('%H:%M', '2026-03-15 14:05:09')'14:05' |
| Seconds since 1970 | EXTRACT(EPOCH FROM DATE '2026-03-15')1773532800 | strftime('%s', '2026-03-15')'1773532800' |
| An impossible date | to_char(DATE '2026-13-01', 'YYYY')error: date/time field value out of range: "2026-13-01" | strftime('%Y', '2026-13-01')NULL |
| NULL | to_char(NULL::date, 'YYYY')NULL | strftime('%Y', NULL)NULL |
Related
Common mistakes
Using strftime in PostgreSQL: it doesn't exist there (see “function does not exist”).
Comparing strftime's text with a number:
strftime('%Y', d) = 2026compares text with an integer and is false. Compare with'2026', or CAST the result.Expecting
%Wto be the ISO week: it counts weeks starting on Monday from the first Monday of the year; PostgreSQL'sIWis the ISO week, so the two disagree around New Year (see the matrix).
PostgreSQL and SQLite
SQLite-only. Its PostgreSQL equivalents are to_char (formatting) and EXTRACT (numbers); every example shows both, and the matrix puts them side by side. Note the different results for week numbers and for an impossible date: SQLite returns NULL, PostgreSQL raises an error.
Official documentation
Go deeper
- Guide: SQL date functions in PostgreSQL and SQLite
- Practice: SQL date practice: 7 exercises in PostgreSQL and SQLite
- Lesson: Working with dates (Pro)
Make it stick in five minutes a day
“Working with dates” teaches this step by step on a real database, with instant feedback. Part of sqlstreak Pro; the first lessons are free.