SQL Dates
Dates are among the trickiest parts of SQL, because every database handles them differently. SQLite has no separate date type: it stores dates as ISO 8601 text ('2025-06-13', '2025-06-13 14:30:00') and provides functions to calculate with them. TAD Market uses exactly that format.
SQLite date functions
| Function | Returns | Example result |
|---|---|---|
date(value, modifiers…) |
YYYY-MM-DD |
2025-06-13 |
time(value, …) |
HH:MM:SS |
14:30:00 |
datetime(value, …) |
YYYY-MM-DD HH:MM:SS |
2025-06-13 14:30:00 |
julianday(value, …) |
day number (real) | 2460839.5 |
strftime(format, value, …) |
custom format | 13/06/2025 |
unixepoch(value, …) |
seconds since 1970 | 1749772800 |
Note:
date('now')returns today's date. Our examples always use fixed dates from the data, so the output is the same whenever you run them.
Modifiers: date arithmetic
Modifiers like '+7 days', '-1 month', 'start of month' and 'weekday 1' shift a date:
SELECT date('2025-06-13', '+7 days') AS next_week,
date('2025-06-13', '-1 month') AS month_ago,
date('2025-06-13', 'start of month') AS month_start,
date('2025-06-13', 'start of month', '+1 month', '-1 day') AS month_end,
date('2025-06-13', 'weekday 1') AS next_monday;next_week | month_ago | month_start | month_end | next_monday
-----------+------------+-------------+------------+------------
2025-06-20 | 2025-05-13 | 2025-06-01 | 2025-06-30 | 2025-06-16
(1 row)
Extracting parts with strftime
| Code | Meaning | Code | Meaning |
|---|---|---|---|
%Y |
year | %H |
hour 00–23 |
%m |
month 01–12 | %M |
minute |
%d |
day 01–31 | %w |
weekday 0–6 (Sunday = 0) |
%j |
day of year | %W |
week of year |
SELECT order_id, order_date,
strftime('%Y', order_date) AS year,
strftime('%m', order_date) AS month,
strftime('%w', order_date) AS weekday,
strftime('%d.%m.%Y', order_date) AS european
FROM orders
WHERE order_id <= 4
ORDER BY order_id;order_id | order_date | year | month | weekday | european
---------+------------+------+-------+---------+-----------
1 | 2025-01-18 | 2025 | 01 | 6 | 18.01.2025
2 | 2025-01-02 | 2025 | 01 | 4 | 02.01.2025
3 | 2025-01-19 | 2025 | 01 | 0 | 19.01.2025
4 | 2025-01-08 | 2025 | 01 | 3 | 08.01.2025
(4 rows)
Differences between dates
Subtract julianday values to get a number of days. How long did delivery take?
SELECT order_id, order_date, shipped_date,
julianday(shipped_date) - julianday(order_date) AS days_to_ship
FROM orders
WHERE shipped_date IS NOT NULL
ORDER BY days_to_ship DESC, order_id
LIMIT 5;order_id | order_date | shipped_date | days_to_ship
---------+------------+--------------+-------------
4 | 2025-01-08 | 2025-01-12 | 4
12 | 2025-03-01 | 2025-03-05 | 4
23 | 2025-06-12 | 2025-06-16 | 4
24 | 2025-06-19 | 2025-06-23 | 4
26 | 2025-07-12 | 2025-07-16 | 4
(5 rows)
Average shipping time per employee:
SELECT employee_id,
COUNT(*) AS shipped_orders,
ROUND(AVG(julianday(shipped_date) - julianday(order_date)), 1) AS avg_days
FROM orders
WHERE shipped_date IS NOT NULL
GROUP BY employee_id
ORDER BY avg_days;employee_id | shipped_orders | avg_days
------------+----------------+---------
4 | 7 | 2.1
9 | 3 | 2.3
6 | 11 | 2.4
5 | 4 | 2.5
(4 rows)
Grouping by period
Orders per quarter, built from the month number:
SELECT strftime('%Y', order_date) || '-Q' ||
((CAST(strftime('%m', order_date) AS INTEGER) + 2) / 3) AS quarter,
COUNT(*) AS orders
FROM orders
GROUP BY quarter
ORDER BY quarter;quarter | orders
--------+-------
2025-Q1 | 12
2025-Q2 | 12
2025-Q3 | 12
2025-Q4 | 4
(4 rows)
How long has each employee been with the company?
Measured on a fixed reference date, 2025-12-31:
SELECT first_name, hire_date,
CAST((julianday('2025-12-31') - julianday(hire_date)) / 365.25 AS INTEGER) AS full_years
FROM employees
ORDER BY hire_date
LIMIT 4;first_name | hire_date | full_years
-----------+------------+-----------
Grace | 2019-03-01 | 6
Victor | 2020-01-15 | 5
Lin | 2020-06-01 | 5
Rosa | 2021-02-10 | 4
(4 rows)
Other databases
| Task | SQLite | MySQL | PostgreSQL | SQL Server |
|---|---|---|---|---|
| Today | date('now') |
CURDATE() |
CURRENT_DATE |
CAST(GETDATE() AS date) |
| Add 7 days | date(d, '+7 days') |
DATE_ADD(d, INTERVAL 7 DAY) |
d + INTERVAL '7 days' |
DATEADD(day, 7, d) |
| Days between | julianday(b) - julianday(a) |
DATEDIFF(b, a) |
b - a |
DATEDIFF(day, a, b) |
| Year | strftime('%Y', d) |
YEAR(d) |
EXTRACT(YEAR FROM d) |
YEAR(d) |
Tip: Always store dates in ISO format (
YYYY-MM-DD). It sorts correctly as text, compares correctly with<andBETWEEN, and every date function understands it.
Test yourself
What does date('2025-01-31', '+1 day') return?
- 2025-01-32
- 2025-02-01
- 2025-01-30
- NULL
How do you compute the number of days between two dates in SQLite?
- b - a
- DATEDIFF(a, b)
- julianday(b) - julianday(a)
- days(b, a)
Which strftime code gives the month?
- %M
- %m
- %n
- %mo