SQL Dates and Times
What you will learn
- Why dates are stored as text in the form
YYYY-MM-DD - How to pick the year, month or weekday with
STRFTIME - How to add or subtract days and months with
DATE - How to find the number of days between two dates with
JULIANDAY - Why the examples use a fixed date instead of "today"
This lesson runs on the sample database you chose in lesson 1.1 (use the Example data picker at the top). Edit any query and press Run. Reset data puts the original rows back.
1. Dates are text
SQLite has no separate date type. A date is stored as text in the ISO format YYYY-MM-DD (year, month, day, with zeros): 2026-03-05. A time adds HH:MM:SS. This format has a useful property: sorting the text sorts the dates, and comparing two dates as text compares them correctly.
SELECT customer_id, joined_on, TYPEOF(joined_on) AS type FROM customers ORDER BY customer_id LIMIT 3;
customer_id joined_on type
----------- ---------- ----
1 2025-10-01 text
2 2024-01-17 text
3 2025-10-17 text
(3 rows)
SELECT customer_id, birth_date, TYPEOF(birth_date) AS type FROM customers ORDER BY customer_id LIMIT 3;
customer_id birth_date type
----------- ---------- ----
1 1956-03-20 text
2 1990-10-10 text
3 1961-11-15 text
(3 rows)
SELECT student_id, birth_date, TYPEOF(birth_date) AS type FROM students ORDER BY student_id LIMIT 3;
student_id birth_date type
---------- ---------- ----
1 2012-04-28 text
2 2012-08-30 text
3 2010-10-09 text
(3 rows)
Always write dates in this order, with leading zeros. 5/3/2026 or 2026-3-5 do not sort or compare correctly and the date functions do not understand them.
In other databases. Many databases have real date types and their own date functions with different names. The idea (a date is a value you can compare, add to and take parts from) is the same; the function names are not. Check the documentation of the database you use.
2. Pick a part with STRFTIME
STRFTIME(format, date) returns part of a date as text. The format uses letters: %Y year, %m month number, %d day of the month, %w weekday (0 is Sunday):
SELECT STRFTIME('%Y', '2026-03-05') AS year, STRFTIME('%m', '2026-03-05') AS month,
STRFTIME('%d', '2026-03-05') AS day, STRFTIME('%w', '2026-03-05') AS weekday, STRFTIME('%Y-%m', '2026-03-05') AS year_month;
year month day weekday year_month
---- ----- --- ------- ----------
2026 03 05 4 2026-03
(1 row)
SELECT STRFTIME('%Y', '2026-03-05') AS year, STRFTIME('%m', '2026-03-05') AS month,
STRFTIME('%d', '2026-03-05') AS day, STRFTIME('%w', '2026-03-05') AS weekday, STRFTIME('%Y-%m', '2026-03-05') AS year_month;
year month day weekday year_month
---- ----- --- ------- ----------
2026 03 05 4 2026-03
(1 row)
SELECT STRFTIME('%Y', '2026-03-05') AS year, STRFTIME('%m', '2026-03-05') AS month,
STRFTIME('%d', '2026-03-05') AS day, STRFTIME('%w', '2026-03-05') AS weekday, STRFTIME('%Y-%m', '2026-03-05') AS year_month;
year month day weekday year_month
---- ----- --- ------- ----------
2026 03 05 4 2026-03
(1 row)
(The result is text, so STRFTIME('%m', ...) is '03'. CAST(... AS INTEGER) from lesson 1.6 turns it into a number.) The same functions work on a column:
SELECT joined_on, STRFTIME('%Y', joined_on) AS year, STRFTIME('%Y-%m', joined_on) AS year_month
FROM customers ORDER BY customer_id LIMIT 5;
joined_on year year_month
---------- ---- ----------
2025-10-01 2025 2025-10
2024-01-17 2024 2024-01
2025-10-17 2025 2025-10
2024-06-10 2024 2024-06
2023-07-04 2023 2023-07
(5 rows)
SELECT joined_on, STRFTIME('%Y', joined_on) AS year, STRFTIME('%Y-%m', joined_on) AS year_month
FROM customers ORDER BY customer_id LIMIT 5;
joined_on year year_month
---------- ---- ----------
2025-05-07 2025 2025-05
2020-01-08 2020 2020-01
2025-03-04 2025 2025-03
2022-10-08 2022 2022-10
2022-06-19 2022 2022-06
(5 rows)
SELECT due_date, STRFTIME('%Y', due_date) AS year, STRFTIME('%w', due_date) AS weekday
FROM assignments ORDER BY assignment_id LIMIT 5;
due_date year weekday
---------- ---- -------
2025-11-16 2025 0
2025-11-11 2025 2
2025-10-24 2025 5
2025-09-14 2025 0
2025-03-27 2025 4
(5 rows)
3. Add and subtract with DATE
DATE(date, modifier, ...) returns a date, moved by modifiers such as '+7 days', '-1 month' or '+1 year':
SELECT DATE('2026-03-05', '+7 days') AS next_week, DATE('2026-03-05', '-1 month') AS month_before,
DATE('2026-03-05', '+1 year') AS next_year, DATE('2026-03-05', 'start of month') AS first_of_month;
next_week month_before next_year first_of_month
---------- ------------ ---------- --------------
2026-03-12 2026-02-05 2027-03-05 2026-03-01
(1 row)
SELECT DATE('2026-03-05', '+7 days') AS next_week, DATE('2026-03-05', '-1 month') AS month_before,
DATE('2026-03-05', '+1 year') AS next_year, DATE('2026-03-05', 'start of month') AS first_of_month;
next_week month_before next_year first_of_month
---------- ------------ ---------- --------------
2026-03-12 2026-02-05 2027-03-05 2026-03-01
(1 row)
SELECT DATE('2026-03-05', '+7 days') AS next_week, DATE('2026-03-05', '-1 month') AS month_before,
DATE('2026-03-05', '+1 year') AS next_year, DATE('2026-03-05', 'start of month') AS first_of_month;
next_week month_before next_year first_of_month
---------- ------------ ---------- --------------
2026-03-12 2026-02-05 2027-03-05 2026-03-01
(1 row)
A made-up rule: a customer's membership review is due one year after joining:
SELECT customer_id, joined_on, DATE(joined_on, '+1 year') AS review_due
FROM customers ORDER BY customer_id LIMIT 5;
customer_id joined_on review_due
----------- ---------- ----------
1 2025-10-01 2026-10-01
2 2024-01-17 2025-01-17
3 2025-10-17 2026-10-17
4 2024-06-10 2025-06-10
5 2023-07-04 2024-07-04
(5 rows)
A made-up rule: a card review is due 30 days after an account was opened:
SELECT account_id, opened_on, DATE(opened_on, '+30 days') AS review_due
FROM accounts ORDER BY account_id LIMIT 5;
account_id opened_on review_due
---------- ---------- ----------
1 2025-05-27 2025-06-26
2 2021-11-26 2021-12-26
3 2024-09-03 2024-10-03
4 2023-05-04 2023-06-03
5 2021-10-12 2021-11-11
(5 rows)
A made-up rule: marks are published 14 days after the due date:
SELECT title, due_date, DATE(due_date, '+14 days') AS marks_published
FROM assignments ORDER BY assignment_id LIMIT 5;
title due_date marks_published
---------- ---------- ---------------
Midterm 2025-11-16 2025-11-30
Project 2025-11-11 2025-11-25
Quiz 1 2025-10-24 2025-11-07
Homework 2 2025-09-14 2025-09-28
Homework 1 2025-03-27 2025-04-10
(5 rows)
4. Days between two dates
Subtracting text dates does not work. JULIANDAY(date) turns a date into a number of days, and the difference of two of them is the number of days between:
SELECT JULIANDAY('2026-03-15') - JULIANDAY('2026-03-05') AS days_between;
days_between
------------
10.0
(1 row)
SELECT JULIANDAY('2026-03-15') - JULIANDAY('2026-03-05') AS days_between;
days_between
------------
10.0
(1 row)
SELECT JULIANDAY('2026-03-15') - JULIANDAY('2026-03-05') AS days_between;
days_between
------------
10.0
(1 row)
How long does an order take to ship? Only orders that have shipped have a shipped_on:
SELECT order_id, order_date, shipped_on, CAST(JULIANDAY(shipped_on) - JULIANDAY(order_date) AS INTEGER) AS days_to_ship
FROM orders WHERE shipped_on IS NOT NULL ORDER BY order_id LIMIT 5;
order_id order_date shipped_on days_to_ship
-------- ---------- ---------- ------------
1 2025-04-23 2025-04-24 1
2 2024-01-21 2024-01-26 5
3 2025-05-03 2025-05-04 1
5 2024-05-26 2024-05-27 1
6 2026-04-06 2026-04-09 3
(5 rows)
How many full years old is each customer on 2026-01-01? (365.25 allows for leap years.)
SELECT full_name, birth_date,
CAST((JULIANDAY('2026-01-01') - JULIANDAY(birth_date)) / 365.25 AS INTEGER) AS age_on_2026_01_01
FROM customers ORDER BY customer_id LIMIT 5;
full_name birth_date age_on_2026_01_01
----------- ---------- -----------------
Chen Park 1956-03-20 69
Rafi Uddin 1990-10-10 35
Grace Kamau 1961-11-15 64
Mei Bianchi 1977-11-18 48
Farid Lee 1974-09-09 51
(5 rows)
How many full years old is each student on 2026-01-01?
SELECT first_name, birth_date,
CAST((JULIANDAY('2026-01-01') - JULIANDAY(birth_date)) / 365.25 AS INTEGER) AS age_on_2026_01_01
FROM students ORDER BY student_id LIMIT 5;
first_name birth_date age_on_2026_01_01
---------- ---------- -----------------
Vera 2012-04-28 13
Elena 2012-08-30 13
Chloe 2010-10-09 15
Quinn 2011-12-07 14
Dana 2010-05-02 15
(5 rows)
5. "Today" and repeatable examples
DATE('now') gives today's date in UTC, so the same query returns a different answer tomorrow. That is right for a real report, but the examples in this course use a fixed date such as '2026-01-01' so that the results shown on the page stay true. Replace it with 'now' when you want today:
SELECT DATE('now') = DATE('now', '+0 days') AS same_day, LENGTH(DATE('now')) AS length_of_a_date;
same_day length_of_a_date
-------- ----------------
1 10
(1 row)
SELECT DATE('now') = DATE('now', '+0 days') AS same_day, LENGTH(DATE('now')) AS length_of_a_date;
same_day length_of_a_date
-------- ----------------
1 10
(1 row)
SELECT DATE('now') = DATE('now', '+0 days') AS same_day, LENGTH(DATE('now')) AS length_of_a_date;
same_day length_of_a_date
-------- ----------------
1 10
(1 row)
Worked example: orders per month
A manager wants to know how many records were created in each month. The recipe uses only what you learned:
- take the year and month with
STRFTIME('%Y-%m', date_column); - give it an alias, for example
year_month; - later (chapter 3) group by it and count.
For now, just look at the first step on real data. The rest comes with GROUP BY. The data is made up.
Common mistakes
- Dates in another order (
05/03/2026). They do not sort or compare; useYYYY-MM-DD. - Subtracting dates as text. Use
JULIANDAYfor the difference. - Forgetting that
STRFTIMEreturns text. - Using
DATE('now')in a lesson or test and expecting the same answer tomorrow. - Assuming other databases have the same function names.
Key takeaways
- SQLite stores dates as ISO text
YYYY-MM-DD; sorting the text sorts the dates. STRFTIMEpicks parts (%Y,%m,%d,%w);DATE(date, '+7 days')moves a date.JULIANDAY(a) - JULIANDAY(b)is the number of days between two dates.- Use a fixed date for repeatable examples and
'now'for today.
Practice
1. What does DATE('2026-03-05', '+10 days') return?
Show answer
2026-03-15.
2. How do you get the year of a date column called joined_on?
Show answer
STRFTIME('%Y', joined_on).
3. What does JULIANDAY('2026-03-10') - JULIANDAY('2026-03-01') return?
Show answer
9, the number of days between the two dates.
4. Why is 2026-3-5 a bad way to write a date?
Show answer
It lacks the leading zeros of 2026-03-05, so it does not sort or compare correctly and the date functions do not understand it.
5. Why do the lessons use '2026-01-01' and not 'now'?
Show answer
'now' changes every day, so the printed results would stop being true. A fixed date gives the same result every time.
6. What does DATE('2026-03-05', 'start of month') return?
Show answer
2026-03-01, the first day of that month.