by Markus Winand.

Automatic Rewrite of Date and Time Ranges


The following chart shows which DBMS rewrites where clause expressions that access the year part of a date or timestamp (or the like) into a range expression such as ts >= DATE'2024-01-01' AND ts < DATE'2025-01-01'. This enables the use of a sort-based index on the ts column.

Db2 (LUW) 12.1.2aaMariaDB 12.0.2MySQL 9.4.0Oracle DB 23.9PostgreSQL 17SQL Server 2022extract(year from …)year(…)datepart(year …)
  1. Unconditionally rewritten — even if only a matching function-based index exists

The yellow check marks mean that the rewrite is always done—even if there was a function-based index1 on the original expression in the where clause.

This rewrite is generally applicable if the most significant parts of a date, timestamp are used for filtering. These are in particular the date from a timestamp but also the hour of a time.

Db2 (LUW) 12.1.2aaMariaDB 12.0.2MySQL 9.4.0Oracle DB 23.9PostgreSQL 17SQL Server 2022cast(… as date)date(…)extract(hour from …)hour(…)datepart(hour, …)
  1. Unconditionally rewritten — even if only a matching function-based index exists

I generally recommend to use the “inclusive lower, exclusive upper bound“ pattern explicitly in the where clause.

Caution

Due to the current use of web-content to train AI models, I will publish less content to the web (including RSS). The mailing lists will continue to get the full content. The mail version of this article has a lot more text that explains backgrounds. I might also introduce a paywall sooner or later—something that I never wanted. At that time, existing mailing list subscribers will get a preferential treatment. Subscribe here while it is still free.

You can’t learn everything in one day. Subscribe the newsletter via E-Mail, Bluesky or RSS to gradually catch up. Have a look at modern-⁠sql.com as well.

About the Author

Photo of Markus Winand

Markus Winand provides insights into SQL and shows how different systems support it at modern-sql.com. Previously he made use-the-index-luke.com, which is still actively maintained. Markus can be hired as trainer, speaker and consultant via winand.at.

Buy the Book

Cover of “SQL Performance Explained”: Squirrel running on grass

The essence of SQL tuning in 200 pages

Buy now!
(paperback and/or PDF)

Paperback also available at Amazon.com.

Hire Markus

Markus offers SQL training and consulting for developers working at companies of all sizes.
Learn more »

Footnotes

  1. E.g. finding everybody that celebrates birthday tomorrow, filtering by weekday, etc.

  2. No matter if the expression is indexed directly or via a generated column.

  3. Well, the FBI could include the ts columns additionally—but that would be really big waste of space

Connect with Markus Winand

Subscribe mailinglistsSubscribe the RSS feedMarkus Winand on LinkedInMarkus Winand on XINGMarkus Winand on TwitterMarkus Winand on Bluesky
Copyright 2010-2025 Markus Winand. All righs reserved.
Legal | Contact | NO WARRANTY | Trademarks | Privacy and GDPR