Session Ready
Exercise

Date math and leap years

Some of you may have experience using R and here we note that leap year date math can be tricky with R and the lubridate package. lubridate has two types of functions: duration and period.

lubridate::ymd(20120229) - lubridate::dyears(4) --> 2008-03-01, which is wrong.

lubridate::ymd(20120229) - lubridate::dyears(1) --> 2011-03-01, which is correct.

lubridate::ymd(20120229) - lubridate::years(4) --> 2008-02-29, which is correct.

lubridate::ymd(20120229) - lubridate::years(1) --> NA, which is unexpected behavior.

We can use the DATEADD() and DATEDIFF() functions to see how SQL Server deals with leap years to see if it has any of the same eccentricities.

Instructions 1/3
undefined XP
  • 1
  • 2
  • 3

Fill in the date parts and intervals needed to determine how SQL Server works on February 29th of a leap year.

2012 was a leap year. The leap year before it was 4 years earlier, and the leap year after it was 4 years later.