Shelley Doll finishes her coverage of SQL data types with an overview of datetime and interval data types. Learn more about the basic syntax of these standard SQL types.
Datetimes and intervals are time-based data types. These types are useful for recording when a database entry was created or modified, for logging when an event occurred, or for determining how much time has elapsed since another datetime value was created. Here, I will explain the SQL92 standard’s descriptions of these time-tracking types.
To learn more about SQL data types, read “SQL basics: String data types,” which covers general data-type usage and describes using the various string data types, and “SQL basics: Number data types,” which details using number data types.
Datetimes
Data types that have values representing times or dates are referred to as datetimes. Each datetime data type has its own means for determining the length of the value and for knowing what information is stored, such as day, month, minutes, seconds, fraction of a second, etc.
Actual datetime implementations vary in length and format from the defined standard; however, each manufacturer’s defined type is expected to conform internally to the rules laid out below. For example, a timestamp implementation may have no delimiters, changing both the length and format from the specification details, which does contain delimiters and in some cases, spaces. The datetime data types include DATE, TIME, and TIMESTAMP. Let’s take a closer look at each, beginning with DATE.
DATE
Usage: DATE
TIME
Usage: TIME (precision)
TIME WITH TIME ZONE
Usage: TIME (precision) WITH TIME ZONE
TIMESTAMP
Usage: TIMESTAMP (precision)
TIMESTAMP WITH TIME ZONE
Usage: TIMESTAMP (precision) WITH TIME ZONE
Intervals
Intervals are used to represent a measure of time. For example, you can use operators (explained below) to find out and store the number of days between two dates.
Manufacturers vary drastically in their handling of intervals—some offer different types for each unit of measure, such as years and minutes, and some databases don’t support intervals at all. The SQL92 interval type definition has only one subtype: INTERVAL.
INTERVAL
Usage: INTERVAL (qualifier)
Operators
SQL92 outlines various operators that can be used on datetime and interval data types. Review Table A below to find possible combinations and the data types these operations result in.
| Operand Type | Operator | 2nd Operand Type | Result Type |
| Datetime | – | Datetime | Interval |
| Datetime | + or – | Interval | Datetime |
| Interval | + | Datetime | Datetime |
| Interval | + or – | Interval | Interval |
| Interval | * or / | Numeric | Interval |
Operation result types
Time zones are maintained when dealing with date times, although some databases will convert one time zone to another to allow comparisons. Some keyword operators exist, such as OVERLAPS and EXTRACT, to manipulate and compare datetime values. However, database implementations are highly varied in their usage or support.
OVERLAPS can be used with two sets of datetimes or a datetime and an interval to determine at what time span the dates intersect. Use EXTRACT to pull one component out of a datetime or interval type, such as to determine just the month value in a DATE.
Data types
As the standard has been designed for manufacturers to base their actually implemented types on the ones presented here, you should always refer to your database’s documentation for specific limits, parameters, and data type names.
If you have a SQL question you’d like to see a solution to, e-mail our editors and your query quandary may be answered in a future SQL article.