SQL Dates

Store and query dates and times.

✍️ 3 exercises + code challenge in this lesson

Dates are usually stored as DATE, DATETIME or TIMESTAMP. The safest text format is ISO: '2026-10-22'.

Date functions differ between databases:

  • Current date: CURRENT_DATE, NOW() (MySQL), GETDATE() (SQL Server), date('now') (SQLite)
  • Parts: YEAR(d) / MONTH(d) (MySQL), EXTRACT(YEAR FROM d) (PostgreSQL), strftime('%Y', d) (SQLite)
  • Arithmetic: DATE_ADD(d, INTERVAL 7 DAY) (MySQL), date(d, '+7 days') (SQLite)

Key Takeaways: SQL Dates

Mastering SQL Dates helps you write clean and reliable SQL code. Follow standard conventions, test edge cases, and keep logic simple. Try running the code example below and complete the exercises to build confidence.

SQL Dates Code Example

SQL
SELECT date('now') AS Today;

SELECT OrderID, OrderDate,
       strftime('%m', OrderDate) AS Month,
       date(OrderDate, '+7 days') AS DeliveryBy,
       julianday('now') - julianday(OrderDate) AS DaysAgo
FROM Orders
WHERE OrderDate >= '2026-06-01';

SELECT strftime('%Y-%m', OrderDate) AS Month, COUNT(*) AS Orders
FROM Orders GROUP BY Month;
Free sign-in required · Google login available

SQL Dates Practice Exercises

Check what you learned in “SQL Dates”.

/ 3 solved
Fill in the blank

Get the month part of a date in SQLite.

strftime('', OrderDate)
✓ Correct! Not quite. Try again.
Multiple choice

Which format is safest for storing dates as text?

✓ Correct! Not quite. Try again.
Code challenge

Select OrderID of orders placed in July 2026 or later and before September 2026, sorted by OrderID.

Write the code in the live editor. Your answer is checked automatically.

Free sign-in required · Google login available

to save your exercise progress.
Finished this lesson?
Mark it complete to track your progress.

Stuck on something?

Ask the community in the CodesInTech forum.

Need a developer?

Learning on your own is great — and when you need professional help, our team builds custom software, websites and mobile apps for clients worldwide.

Explore Our Services