SQL Dates

Working with dates in SQL means knowing the available date and time types and the functions that read, compare, and format them.

How Databases Store Dates

Dates and times are stored in dedicated column types rather than as plain text. Storing them this way lets the database compare them correctly, sort them in true chronological order, and calculate the distance between two points in time. If you store a date as a string instead, sorting and comparison can go wrong because '2024-1-9' and '2024-10-1' do not order the way you expect as text.

The most common types are shown below. The exact set and their names vary a little between database systems, but the ideas are consistent.

TypeStoresExample value
DATECalendar date only2026-07-15
TIMETime of day only14:30:00
DATETIMEDate and time together2026-07-15 14:30:00
TIMESTAMPDate and time, often tracked in UTC2026-07-15 14:30:00
YEARA year value (MySQL)2026
Note: The recommended, portable way to write a date literal is the ISO format 'YYYY-MM-DD', with time as 'YYYY-MM-DD HH:MM:SS'. Using this order avoids the ambiguity of formats like month/day versus day/month.

Filtering by Date

Because dates are true values, you can compare them with the usual operators and with BETWEEN. The query below selects orders placed on a single day.

Select rows for one date

SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate = '2026-07-15';

Select a range of dates

SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate BETWEEN '2026-01-01' AND '2026-03-31';
Note: If a column holds a full DATETIME with a time part, comparing it to a plain date like '2026-07-15' matches only the exact moment at midnight, so rows later in the day are missed. To catch a whole day, filter on a range: OrderDate >= '2026-07-15' AND OrderDate < '2026-07-16'.

Common Date and Time Functions

Every database provides functions to fetch the current moment, extract parts of a date, and do date arithmetic. Names differ by dialect, so check your system's reference for exact spellings.

PurposeMySQLSQL Server
Current dateCURDATE()CAST(GETDATE() AS DATE)
Current date and timeNOW()GETDATE()
Extract a part (year, month, day)EXTRACT(YEAR FROM col)DATEPART(year, col)
Add an intervalDATE_ADD(col, INTERVAL 7 DAY)DATEADD(day, 7, col)
Difference between datesDATEDIFF(a, b)DATEDIFF(day, b, a)
Format a date as textDATE_FORMAT(col, '%Y-%m-%d')FORMAT(col, 'yyyy-MM-dd')

Use a date function

SELECT OrderID, OrderDate
FROM Orders
WHERE OrderDate >= DATE_ADD(CURDATE(), INTERVAL -30 DAY);

Tips for Working with Dates

  • Store dates in a date or datetime type, never as free text.
  • Prefer the ISO 'YYYY-MM-DD' format for literals to stay portable.
  • Store and compare timestamps in UTC when your application spans time zones, and convert for display only.
  • Be careful mixing DATE and DATETIME values, since a hidden time component can quietly exclude rows from a filter.

Exercise: SQL Dates

What is the standard, unambiguous format for representing dates in SQL?