9.5. Calendar Tables

Calendar tables are necessary because the current calendar is so irregular that you cannot reasonably compute events. Yes, there is a 19-year cycle (the Metonic cycle, named after the fifth-century Greek astronomer, Meton) in which the days of the week will repeat and you could use this to create a self-updating view if you need to use the days of the week. But it is just as easy to have a 100-year calendar in a single short table and use it.

9.5.1. Day of Week via Tables

The classic method for computing the day of the week in the Common Era calendar is a bit complicated. Here is the algorithm:

  1. Take the last two digits of the year. In our example, this is 82.

  2. Do integer division by 4 on the decade (two digits of the year). ...

Get Joe Celko's Thinking in Sets: Auxiliary, Temporal, and Virtual Tables in SQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.