sqlstreak

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

PostgreSQLSQLite
AvailableNo: use to_char() or EXTRACT()Yes, built in
Syntaxto_char(date_or_timestamp, 'YYYY-MM') -- the PostgreSQL equivalentstrftime(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

namearrived_onyearmonth
Tofu2025-04-04202504
Gizmo2025-05-23202505
Bruno2025-06-30202506
Rocket2025-07-09202507
Socks2025-08-17202508

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

weekdayorders
06
22
33
43
51
62

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

monthsignups
2026-0180
2026-02100
2026-03120

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.

CasePostgreSQLSQLite
Yearto_char(DATE '2026-03-15', 'YYYY')'2026'strftime('%Y', '2026-03-15')'2026'
Month, zero-paddedto_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')0strftime('%w', '2026-03-15')'0'
Day of the yearto_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 timestampto_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 1970EXTRACT(EPOCH FROM DATE '2026-03-15')1773532800strftime('%s', '2026-03-15')'1773532800'
An impossible dateto_char(DATE '2026-13-01', 'YYYY')error: date/time field value out of range: "2026-13-01"strftime('%Y', '2026-13-01')NULL
NULLto_char(NULL::date, 'YYYY')NULLstrftime('%Y', NULL)NULL

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) = 2026 compares text with an integer and is false. Compare with '2026', or CAST the result.

  • Expecting %W to be the ISO week: it counts weeks starting on Monday from the first Monday of the year; PostgreSQL's IW is 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

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.

Start free