Academic Block

SQL DATES Get Certified

Learn how to store, retrieve, compare, format and calculate dates in SQLite using the built-in date and time functions.

What are SQL Dates?

Dates are used for registration dates, order dates, delivery dates, birthdays, event schedules and many other business values. SQLite does not have a dedicated DATE type. Dates are usually stored as TEXT (most common), REAL or INTEGER.

For almost all applications the ISO-8601 text format YYYY-MM-DD is the most convenient choice because it sorts correctly and works perfectly with SQLite’s date functions.

Basic Date Table

What this code does: Creates an events table, stores three dates in YYYY-MM-DD format and lists them in chronological order.

DROP TABLE IF EXISTS events;
CREATE TABLE events (
  event_id INTEGER PRIMARY KEY,
  event_name TEXT NOT NULL,
  event_date TEXT NOT NULL
);
INSERT INTO events (event_name, event_date) VALUES
  ('Tech Workshop', '2026-09-10'),
  ('Database Seminar', '2026-10-05'),
  ('Science Exhibition', '2026-11-18');
SELECT *
FROM events
ORDER BY event_date;

Current Date

date('now') returns the current date in UTC using the YYYY-MM-DD format.

SELECT date('now') AS current_date;

Current Date and Time

Use datetime('now') when you need both the date and the time.

SELECT datetime('now') AS current_datetime;

Extracting Year, Month and Day

strftime() extracts any part of a date. The format codes %Y, %m and %d give year, month and day.

SELECT
  strftime('%Y', '2026-08-27') AS year,
  strftime('%m', '2026-08-27') AS month,
  strftime('%d', '2026-08-27') AS day;

Adding Days to a Date

Pass a modifier such as '+7 days' to date() to move forward in time.

SELECT
  date('2026-08-27', '+7 days') AS date_after_7_days;

Subtracting Days from a Date

A negative modifier calculates an earlier date.

SELECT
  date('2026-08-27', '-10 days') AS date_10_days_earlier;

Adding Months

Month modifiers work the same way.

SELECT
  date('2026-08-27', '+3 months') AS date_after_3_months;

Comparing Dates

Because ISO dates sort lexicographically, ordinary comparison operators work correctly.

What this code does: Returns only the deliveries that occur after 27 August 2026.

DROP TABLE IF EXISTS deliveries;
CREATE TABLE deliveries (
  delivery_id INTEGER PRIMARY KEY,
  customer_name TEXT NOT NULL,
  delivery_date TEXT NOT NULL
);
INSERT INTO deliveries (customer_name, delivery_date) VALUES
  ('Aarav', '2026-08-20'),
  ('Meera', '2026-09-02'),
  ('Kabir', '2026-08-28'),
  ('Nisha', '2026-09-15');
SELECT
  customer_name,
  delivery_date
FROM deliveries
WHERE delivery_date > '2026-08-27'
ORDER BY delivery_date;

Dates Between Two Values

BETWEEN selects an inclusive date range.

What this code does: Finds appointments that fall between 15 and 31 August 2026.

DROP TABLE IF EXISTS appointments;
CREATE TABLE appointments (
  appointment_id INTEGER PRIMARY KEY,
  patient_name TEXT NOT NULL,
  appointment_date TEXT NOT NULL
);
INSERT INTO appointments (patient_name, appointment_date) VALUES
  ('Aditi', '2026-08-10'),
  ('Rohan', '2026-08-18'),
  ('Sana', '2026-08-25'),
  ('Karan', '2026-09-03');
SELECT
  patient_name,
  appointment_date
FROM appointments
WHERE appointment_date BETWEEN '2026-08-15' AND '2026-08-31'
ORDER BY appointment_date;

Formatting Dates with strftime()

You can re-format a date for display.

SELECT
  strftime('%d-%m-%Y', '2026-08-27') AS formatted_date;

Day of the Week

%w returns the weekday number (0 = Sunday … 6 = Saturday).

SELECT
  strftime('%w', '2026-08-27') AS weekday_number;

Start of the Month

The modifier 'start of month' returns the first day of the month that contains the given date.

SELECT
  date('2026-08-27', 'start of month') AS first_day_of_month;

Start of the Year

'start of year' returns 1 January of the same year.

SELECT
  date('2026-08-27', 'start of year') AS first_day_of_year;

Difference Between Two Dates

julianday() converts a date into a Julian day number. Subtracting two Julian numbers gives the number of days between the dates.

SELECT
  julianday('2026-09-15') - julianday('2026-09-01')
  AS days_difference;

Using Date Functions with Table Columns

Date functions work equally well on columns stored in a table.

What this code does: Calculates a renewal date 30 days after each subscription start date.

DROP TABLE IF EXISTS subscriptions;
CREATE TABLE subscriptions (
  subscription_id INTEGER PRIMARY KEY,
  customer_name TEXT NOT NULL,
  start_date TEXT NOT NULL
);
INSERT INTO subscriptions (customer_name, start_date) VALUES
  ('Isha', '2026-07-05'),
  ('Dev', '2026-08-12'),
  ('Nisha', '2026-09-20');
SELECT
  customer_name,
  start_date,
  date(start_date, '+30 days') AS renewal_date
FROM subscriptions
ORDER BY start_date;

Common SQLite Date Functions

Function
Purpose
Example
date()
Returns a date
date(‘now’)
time()
Returns a time
time(‘now’)
datetime()
Returns date and time
datetime(‘now’)
julianday()
Returns a Julian day number
julianday(‘2026-08-27’)
strftime()
Formats date/time values
strftime(‘%Y’, ‘2026-08-27’)

Common Date Modifiers

Modifier
Purpose
Example
+7 days
Add seven days
date(‘2026-08-27’, ‘+7 days’)
-1 month
Subtract one month
date(‘2026-08-27’, ‘-1 month’)
+1 year
Add one year
date(‘2026-08-27’, ‘+1 year’)
start of month
First day of the month
date(‘2026-08-27’, ‘start of month’)
start of year
First day of the year
date(‘2026-08-27’, ‘start of year’)

Advantages of Working with Dates

  • Store and organise calendar information cleanly.
  • Compare dates with ordinary operators.
  • Perform date arithmetic (add or subtract days, months, years).
  • Filter records inside date ranges.
  • Format dates for display with strftime().

Best Practices

  • Always store dates as TEXT in the YYYY-MM-DD format when possible.
  • Keep the format consistent across the whole table.
  • Prefer the built-in functions date(), datetime(), strftime() and julianday().
  • Remember that ‘now’ is evaluated in UTC.
  • Do not expect a native DATE storage type the way some other database systems provide one.

SQLite’s date and time functions give you everything you need to store, compare, format and calculate dates. Master date(), datetime(), strftime() and julianday() and you can handle almost any calendar-related requirement. Click Test My Code on any example to run it live, or Code Explanation to walk through the query block by block.

Ctrl + Enter to run code in the editor Esc to close the editor

🧪 Test Your SQL Code

Edit the SQL code on the left and click “Run Code” to see the result on the right.

📝 SQL Code
👁️ Preview (query result)

Click “Run Code” to see the result here.