17.10 - Argument Types and Rules - Advanced SQL Engine - Teradata Database

Teradata Vantage™ - SQL Date and Time Functions and Expressions

Product
Advanced SQL Engine
Teradata Database
Release Number
17.10
Release Date
July 2021
Content Type
Programming Reference
Publication ID
B035-1211-171K
Language
English (United States)

Expressions passed to this function must have one of the following data types:

DATE, TIMESTAMP, or TIMESTAMP WITH TIME ZONE

The two argument to MONTHS_BETWEEN can be one of the above three data types, but the two arguments must be the same data type. For example, you cannot pass a DATE as the first parameter and a TIMESTAMP as the second parameter.

Since TIMESTAMP values are stored in UTC time within the database and lack a time zone, the session time zone is used to interpret the time stamp value within the function. For TIMESTAMP WITH TIME ZONE values, the time zone component is used to interpret the time stamp value within the function.

If date_timestamp_value2 is:

  • Later than date_timestamp_value1, the result is negative.
  • Earlier than date_timestamp_value1, the result is positive.

If date_timestamp_value1 and date_timestamp_value2 are either the same days of the month or both last days of months, the result is always an integer. Otherwise, MONTHS_BETWEEN calculates the fractional portion of the result based on a 31-day month and considers the difference in time components of date_timestamp_value1 and date_timestamp_value2. The Gregorian calendar is supported.

Timestamps without a time zone are compared in the local time zone. This matters solely for the purpose of determining if two timestamps reside on the same day or are both the last day of a month. Timestamps with a time zone are converted to UTC before being compared in order to ensure the times are in the same zone.