How to Filter Dates in SQL (Last N Months, DATETIME → DATE)

Filter recent rows, convert DATETIME to DATE, and write clear date ranges — with examples you can reuse on order dates.

all rows WHERE kept rows
WHERE keeps only the rows that match your condition.

Filter a date column with a range

SELECT order_id, order_date, status
FROM orders
WHERE order_date >= '2024-01-01'
  AND order_date <  '2025-01-01';

Prefer half-open ranges >= start AND < next_period so you don’t double-count boundary midnights.

“Last N months” (idea, dialect differs)

The idea is always: compare to “today minus N months.” Syntax changes by engine:

In practice: filter on the date column itself when you can, and be careful with time zones if your database stores timestamps in UTC.

-- Paid orders in January 2024 (half-open range)
SELECT order_id, order_date
FROM orders
WHERE status = 'paid'
  AND order_date >= '2024-01-01'
  AND order_date <  '2024-02-01';

DATETIME → DATE

When a column stores time-of-day but you only care about the calendar day, cast/truncate:

-- concept
SELECT DATE(order_date) AS order_day, COUNT(*)
FROM orders
GROUP BY DATE(order_date);

Exact function names vary — the business idea doesn’t.

FAQ

How do I get the last 6 months of data in SQL?

Filter with a date range relative to today (syntax varies by database). Conceptually: WHERE order_date >= today minus 6 months.

Try it yourself

The fastest way to lock this in is a short drill on the same Northstar Market tables.

Practice BETWEEN ranges

Related guides