Knowledge base / SQL

SQL YEAR: missing dates and complete-year boundaries

I explain dialects, missing dates, grouping by year and a range that retains late orders on December 31.

Kajo RudzińskiPublished Updated 4 min read

Date tiles enter a selected interval on a timeline.

I want to count orders by year. First I identify the database engine and the date column’s type. YEAR is convenient in SQL Server and MySQL, but it is not universal syntax for every SQL database. Extracting a year for display also differs from selecting a complete date range.

Use the syntax for your engine

In SQL Server, YEAR returns an int. This T-SQL example explicitly establishes the input type.

SELECT YEAR(CAST('20241231' AS date)) AS order_year;

The result is 2024. MySQL 8.4 also provides YEAR, for example YEAR('2024-12-31'). That does not make every date conversion expression interchangeable between the two engines.

Engine Example with an explicit date
SQL Server YEAR(CAST('20241231' AS date))
MySQL 8.4 YEAR('2024-12-31')
PostgreSQL 18 EXTRACT(YEAR FROM DATE '2024-12-31')
SQLite CAST(strftime('%Y', '2024-12-31') AS INTEGER)

The alternatives are documented by PostgreSQL and SQLite. Each expression represents 2024, although their return types can differ. SQLite strftime returns text, so that example includes an integer cast.

Work from a small date column

Assume a synthetic orders table whose order_date column is datetime2 in SQL Server. Each row is one order. Times are already expressed in the agreed reporting time zone.

order_id order_date
1 2024-01-01 00:00:00
2 2024-12-31 23:59:59
3 2025-01-01 00:00:00
4 NULL
SELECT order_id, YEAR(order_date) AS order_year
FROM orders
ORDER BY order_id;

The first two orders belong to 2024, the third to 2025, and the fourth has no year. Null does not indicate year zero. I avoid substituting the current year because that would invent information absent from the source.

Decide separately how to group and handle missing dates

SELECT YEAR(order_date) AS order_year, COUNT(*) AS order_count
FROM orders
WHERE order_date IS NOT NULL
GROUP BY YEAR(order_date)
ORDER BY order_year;

For the stated data, 2024 has count 2 and 2025 has count 1. WHERE deliberately excludes the fourth order. If the report must reconcile all orders, that missing date still needs to be visible in a separate quality check.

For a missing-year label, I keep types compatible. SQL Server COALESCE follows data type precedence. Combining the integer from YEAR with Unknown text can cause a conversion error.

SELECT order_id,
       COALESCE(CAST(YEAR(order_date) AS varchar(10)), 'Unknown') AS year_label
FROM orders
ORDER BY order_id;

Both alternatives are now text. The expected labels are 2024, 2024, 2025 and Unknown. I retain a numeric year separately for calculations; the label is a presentation choice.

Select the complete year with a half-open range

For 2024, I include its start and exclude the start of the next year. This query uses the example’s datetime2 column.

SELECT order_id, order_date
FROM orders
WHERE order_date >= CAST('20240101' AS datetime2)
  AND order_date <  CAST('20250101' AS datetime2)
ORDER BY order_id;

Orders 1 and 2 remain. Order 2 occurs late on December 31 but still precedes the beginning of 2025. Order 3 does not satisfy the upper bound. These expectations follow directly from the stated timestamps and comparisons.

BETWEEN includes both endpoints. An upper endpoint at midnight at the start of December 31 excludes later times that day. I avoid guessing a final fraction of a second because datetime2 has defined fractional precision. Using the next year’s start avoids that boundary problem.

A highlighted time interval ends before the next boundary.

Recognise what the year alone cannot establish

Subtracting calendar-year numbers does not necessarily calculate completed years of age or service. December 31 to January 1 is one day, even though the year number increases by one. The correct calculation needs the relevant definition.

For timestamps, I also establish the time zone before extracting a year. A reporting year in UTC may differ from one in local time around New Year’s Eve. This small example assumes an agreed time representation and does not demonstrate time-zone conversion.

I do not promise that a range condition forces index use or always makes a query faster. In a particular environment, I inspect the plan and index definitions. The half-open range primarily gives this example a clear and complete period definition.

Exercise and next step

On paper, add a record exactly at the start of 2024, another one minute before it, and another exactly at the start of 2025. Identify which records satisfy the range condition. Then explain where orders without dates should appear.

Separating year extraction, missing-value labels and complete-period selection gives you a useful basis for annual reporting. For other date components, continue with EXTRACT.

Key takeaways

Does YEAR work in every SQL engine?

No. YEAR here covers SQL Server and MySQL. PostgreSQL uses EXTRACT, and the SQLite example uses strftime with a cast.

How can I display Unknown for a missing year?

In SQL Server I cast the numeric year to text before using COALESCE with a text label. I retain a numeric year separately for calculations.

How do I include the whole of December 31?

Use an inclusive start of the year and an exclusive start of the next year. An upper bound at midnight at the start of December 31 does not include that whole day.

AI was used in an automated way while working on this article. If you want to learn how to do it too, check out this project.

Kajo Rudziński

Newsletter KajoData

Let’s keep in touch.

I write about data analysis, learning and changing careers. My newsletter and the sign-up form are in Polish. If that works for you, I’d be glad to see you there.