SQL Date Functions

Date functions answer questions like “orders per month” and “days since signup”. This is the area where databases differ most, so each example notes the equivalents.

Updated December 1, 2026

Syntax

strftime('%Y-%m', date)          -- format or extract parts
date(date, '+7 days')            -- date arithmetic
julianday(end) - julianday(start) -- difference in days

Group by month

strftime formats a date. PostgreSQL uses DATE_TRUNC('month', d) or TO_CHAR, MySQL DATE_FORMAT, SQL Server FORMAT.

SELECT strftime('%Y-%m', signed_up) AS month, COUNT(*) AS signups
FROM signups
GROUP BY month
ORDER BY month;
Result on the Acme Co. (company data) sample data (2 rows)
monthsignups
2024-017
2024-021

Add days to a date

When does each order’s 30-day return window close? PostgreSQL writes d + INTERVAL '30 days', MySQL DATE_ADD(d, INTERVAL 30 DAY), SQL Server DATEADD(day, 30, d).

SELECT id, ordered_on, date(ordered_on, '+30 days') AS return_by
FROM orders
WHERE status = 'delivered';
Result on the Desk & Co. (online store) sample data (7 rows, first 5 shown)
idordered_onreturn_by
12025-02-012025-03-03
22025-02-032025-03-05
32025-02-152025-03-17
62025-03-202025-04-19
72025-04-022025-05-02

Difference between dates

Years of service as of January 1, 2026. Elsewhere: DATE_PART or subtraction in PostgreSQL, DATEDIFF in MySQL and SQL Server.

SELECT name, hired,
       ROUND((julianday('2026-01-01') - julianday(hired)) / 365.25, 1) AS years
FROM employees
ORDER BY years DESC;
Result on the Acme Co. (company data) sample data (15 rows, first 5 shown)
namehiredyears
Maya Chen2018-03-127.8
Jack Miller2018-09-037.3
Omar Haddad2019-07-016.5
Diego Alvarez2019-11-116.1
Lena Park2020-01-156

Common mistakes

Practice Date Functions interactively

Write real queries in your browser and get instant feedback in the lesson “Working with dates” (Unit 7). New learners can start free from the first lesson.

Frequently asked questions

How do I get today’s date in SQL?

date('now') in SQLite, CURRENT_DATE in PostgreSQL and MySQL, GETDATE() or CAST(GETDATE() AS date) in SQL Server.

How do I extract the year?

strftime('%Y', d) in SQLite; EXTRACT(YEAR FROM d) in PostgreSQL and MySQL; YEAR(d) in MySQL and SQL Server.

Related SQL tutorials