4.3. Queries with Date Arithmetic

Almost every SQL implementation has a DATE data type, but the functions available for them vary quite a bit. The most common ones are a constructor that builds a date from integers or strings; extractors to pull out the month, day, or year; and some display options to format output.

You can assume that your SQL implementation has simple date arithmetic functions, although with different syntax from product to product, such as:

1.A date plus or minus a number of days yields a new date.
2.A date minus a second date yields an integer number of days.

Table 4.1 displays the valid combinations of <datetime> and <interval> data types in Standard SQL:

Table 4.1. Valid Combinations of <datetime> and <interval> Data Types ...

Get Joe Celko's SQL for Smarties, 3rd Edition 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.