Impala Date and Time Functions

The underlying Impala data type for date and time data is TIMESTAMP, which has both a date and a time portion. Functions that extract a single field, such as hour() or minute(), typically return an integer value. Functions that format the date portion, such as date_add() or to_date(), typically return a string value.

You can also adjust a TIMESTAMP value by adding or subtracting an INTERVAL expression. See TIMESTAMP Data Type for details. INTERVAL expressions are also allowed as the second argument for the date_add() and date_sub() functions, rather than integers.

Some of these functions are affected by the setting of the ‑‑use_local_tz_for_unix_timestamp_conversions startup flag for the impalad daemon. This setting is off by default, meaning that functions such as from_unixtime() and unix_timestamp() consider the input values to always represent the UTC time zone. This setting also applies when you CAST() a BIGINT value to TIMESTAMP, or a TIMESTAMP value to BIGINT. When this setting is enabled, these functions and operations convert to and from values representing the local time zone. See TIMESTAMP Data Type for details about how Impala handles time zone considerations for the TIMESTAMP data type.

Function reference:

Impala supports the following data and time functions:

ADD_MONTHS(TIMESTAMP date, INT months), ADD_MONTHS(TIMESTAMP date, BIGINT months)
Purpose: Returns the specified date and time plus some number of months.

Return type: TIMESTAMP

Usage notes:

Same as MONTHS_ADD(). Available in Impala 1.4 and higher. For compatibility when porting code with vendor extensions.

Examples:

The following examples demonstrate adding months to construct the same day of the month in a different month; how if the current day of the month does not exist in the target month, the last day of that month is substituted; and how a negative argument produces a return value from a previous month.

select now(), add_months(now(), 2);
+-------------------------------+-------------------------------+
| now()                         | add_months(now(), 2)          |
+-------------------------------+-------------------------------+
| 2016-05-31 10:47:00.429109000 | 2016-07-31 10:47:00.429109000 |
+-------------------------------+-------------------------------+

select now(), add_months(now(), 1);
+-------------------------------+-------------------------------+
| now()                         | add_months(now(), 1)          |
+-------------------------------+-------------------------------+
| 2016-05-31 10:47:14.540226000 | 2016-06-30 10:47:14.540226000 |
+-------------------------------+-------------------------------+

select now(), add_months(now(), -1);
+-------------------------------+-------------------------------+
| now()                         | add_months(now(), -1)         |
+-------------------------------+-------------------------------+
| 2016-05-31 10:47:31.732298000 | 2016-04-30 10:47:31.732298000 |
+-------------------------------+-------------------------------+
ADDDATE(TIMESTAMP startdate, INT days), ADDDATE(TIMESTAMP startdate, BIGINT days)
Purpose: Adds a specified number of days to a TIMESTAMP value. Similar to DATE_ADD(), but starts with an actual TIMESTAMP value instead of a string that is converted to a TIMESTAMP.

Return type: TIMESTAMP

Examples:

The following examples show how to add a number of days to a TIMESTAMP. The number of days can also be negative, which gives the same effect as the subdate() function.

select now() as right_now, adddate(now(), 30) as now_plus_30;
+-------------------------------+-------------------------------+
| right_now                     | now_plus_30                   |
+-------------------------------+-------------------------------+
| 2016-05-20 10:23:08.640111000 | 2016-06-19 10:23:08.640111000 |
+-------------------------------+-------------------------------+

select now() as right_now, adddate(now(), -15) as now_minus_15;
+-------------------------------+-------------------------------+
| right_now                     | now_minus_15                  |
+-------------------------------+-------------------------------+
| 2016-05-20 10:23:38.214064000 | 2016-05-05 10:23:38.214064000 |
+-------------------------------+-------------------------------+
CURRENT_TIMESTAMP()
Purpose: Alias for the NOW() function.

Return type: TIMESTAMP

Examples:

select now(), current_timestamp();
+-------------------------------+-------------------------------+
| now()                         | current_timestamp()           |
+-------------------------------+-------------------------------+
| 2016-05-19 16:10:14.237849000 | 2016-05-19 16:10:14.237849000 |
+-------------------------------+-------------------------------+

select current_timestamp() as right_now,
  current_timestamp() + interval 3 hours as in_three_hours;
+-------------------------------+-------------------------------+
| right_now                     | in_three_hours                |
+-------------------------------+-------------------------------+
| 2016-05-19 16:13:20.017117000 | 2016-05-19 19:13:20.017117000 |
+-------------------------------+-------------------------------+
DATE_ADD(TIMESTAMP startdate, INT days), DATE_ADD(TIMESTAMP startdate, interval_expression)
Purpose: Adds a specified number of days to a TIMESTAMP value. With an INTERVAL expression as the second argument, you can calculate a delta value using other units such as weeks, years, hours, seconds, and so on; see TIMESTAMP Data Type for details.

Return type: TIMESTAMP

Examples:

The following example shows the simplest usage, of adding a specified number of days to a TIMESTAMP value:

select now() as right_now, date_add(now(), 7) as next_week;
+-------------------------------+-------------------------------+
| right_now                     | next_week                     |
+-------------------------------+-------------------------------+
| 2016-05-20 11:03:48.687055000 | 2016-05-27 11:03:48.687055000 |
+-------------------------------+-------------------------------+

The following examples show the shorthand notation of an INTERVAL expression, instead of specifying the precise number of days. The INTERVAL notation also lets you work with units smaller than a single day.

select now() as right_now, date_add(now(), interval 3 weeks) as in_3_weeks;
+-------------------------------+-------------------------------+
| right_now                     | in_3_weeks                    |
+-------------------------------+-------------------------------+
| 2016-05-20 11:05:39.173331000 | 2016-06-10 11:05:39.173331000 |
+-------------------------------+-------------------------------+

select now() as right_now, date_add(now(), interval 6 hours) as in_6_hours;
+-------------------------------+-------------------------------+
| right_now                     | in_6_hours                    |
+-------------------------------+-------------------------------+
| 2016-05-20 11:13:51.492536000 | 2016-05-20 17:13:51.492536000 |
+-------------------------------+-------------------------------+

Like all date/time functions that deal with months, date_add() handles nonexistent dates past the end of a month by setting the date to the last day of the month. The following example shows how the nonexistent date April 31st is normalized to April 30th:

select date_add(cast('2016-01-31' as timestamp), interval 3 months) as 'april_31st';
+---------------------+
| april_31st          |
+---------------------+
| 2016-04-30 00:00:00 |
+---------------------+
DATE_PART(STRING a, TIMESTAMP timestamp)
Purpose: Similar to EXTRACT(), with the argument order reversed. Supports the same date and time units as EXTRACT(). For compatibility with SQL code containing vendor extensions.

Return type: bigint

Examples:

select date_part('year',now()) as current_year;
+--------------+
| current_year |
+--------------+
| 2016         |
+--------------+

select date_part('hour',now()) as hour_of_day;
+-------------+
| hour_of_day |
+-------------+
| 11          |
+-------------+
DATE_SUB(TIMESTAMP startdate, INT days), DATE_SUB(TIMESTAMP startdate, interval_expression)
Purpose: Subtracts a specified number of days from a TIMESTAMP value. With an INTERVAL expression as the second argument, you can calculate a delta value using other units such as weeks, years, hours, seconds, and so on; see TIMESTAMP Data Type for details.

Return type: TIMESTAMP

Examples:

The following example shows the simplest usage, of subtracting a specified number of days from a TIMESTAMP value:

select now() as right_now, date_sub(now(), 7) as last_week;
+-------------------------------+-------------------------------+
| right_now                     | last_week                     |
+-------------------------------+-------------------------------+
| 2016-05-20 11:21:30.491011000 | 2016-05-13 11:21:30.491011000 |
+-------------------------------+-------------------------------+

The following examples show the shorthand notation of an INTERVAL expression, instead of specifying the precise number of days. The INTERVAL notation also lets you work with units smaller than a single day.

select now() as right_now, date_sub(now(), interval 3 weeks) as 3_weeks_ago;
+-------------------------------+-------------------------------+
| right_now                     | 3_weeks_ago                   |
+-------------------------------+-------------------------------+
| 2016-05-20 11:23:05.176953000 | 2016-04-29 11:23:05.176953000 |
+-------------------------------+-------------------------------+

select now() as right_now, date_sub(now(), interval 6 hours) as 6_hours_ago;
+-------------------------------+-------------------------------+
| right_now                     | 6_hours_ago                   |
+-------------------------------+-------------------------------+
| 2016-05-20 11:23:35.439631000 | 2016-05-20 05:23:35.439631000 |
+-------------------------------+-------------------------------+

Like all date/time functions that deal with months, DATE_ADD() handles nonexistent dates past the end of a month by setting the date to the last day of the month. The following example shows how the nonexistent date April 31st is normalized to April 30th:

select date_sub(cast('2016-05-31' as timestamp), interval 1 months) as 'april_31st';
+---------------------+
| april_31st          |
+---------------------+
| 2016-04-30 00:00:00 |
+---------------------+
DATE_TRUNC(STRING unit, TIMESTAMP timestamp)
Purpose: Truncates a TIMESTAMP value to the specified precision.

Unit argument: The unit argument value for truncating TIMESTAMP values is not case-sensitive. This argument string can be one of:

  • microseconds
  • milliseconds
  • second
  • minute
  • hour
  • day
  • week
  • month
  • year
  • decade
  • century
  • millennium

For example, calling date_trunc('hour',ts) truncates ts to the beginning of the corresponding hour, with all minutes, seconds, milliseconds, and so on set to zero. Calling date_trunc('milliseconds',ts) truncates ts to the beginning of the corresponding millisecond, with all microseconds and nanoseconds set to zero.

Added in: CDH 5.14.0 / Impala 2.11.0

Usage notes:

Although this function is similar to calling TRUNC() with a TIMESTAMP argument, the order of arguments and the recognized units are different between TRUNC() and DATE_TRUNC(). Therefore, these functions are not interchangeable.

This function is typically used in GROUP BY queries to aggregate results from the same hour, day, week, month, quarter, and so on. You can also use this function in an INSERT ... SELECT into a partitioned table to divide TIMESTAMP values into the correct partition.

Because the return value is a TIMESTAMP, if you cast the result of DATE_TRUNC() to STRING, you will often see zeroed-out portions such as 00:00:00 in the time field. If you only need the individual units such as hour, day, month, or year, use the EXTRACT() function instead. If you need the individual units from a truncated TIMESTAMP value, run the TRUNCATE() function on the original value, then run EXTRACT() on the result.

Return type: TIMESTAMP

Examples:

The following examples show how to call DATE_TRUNC() with different unit values:

select now(), date_trunc('second', now());
+-------------------------------+-----------------------------------+
| now()                         | date_trunc('second', now())       |
+-------------------------------+-----------------------------------+
| 2017-12-05 13:58:04.565403000 | 2017-12-05 13:58:04               |
+-------------------------------+-----------------------------------+

select now(), date_trunc('hour', now());
+-------------------------------+---------------------------+
| now()                         | date_trunc('hour', now()) |
+-------------------------------+---------------------------+
| 2017-12-05 13:59:01.884459000 | 2017-12-05 13:00:00       |
+-------------------------------+---------------------------+

select now(), date_trunc('millennium', now());
+-------------------------------+---------------------------------+
| now()                         | date_trunc('millennium', now()) |
+-------------------------------+---------------------------------+
| 2017-12-05 14:00:30.296812000 | 2000-01-01 00:00:00             |
+-------------------------------+---------------------------------+
DATEDIFF(TIMESTAMP enddate, TIMESTAMP startdate)
Purpose: Returns the number of days between two TIMESTAMP values.

Return type: INT

Usage notes:

If the first argument represents a later date than the second argument, the return value is positive. If both arguments represent the same date, the return value is zero. The time portions of the TIMESTAMP values are irrelevant. For example, 11:59 PM on one day and 12:01 on the next day represent a datediff() of -1 because the date/time values represent different days, even though the TIMESTAMP values differ by only 2 minutes.

Examples:

The following example shows how comparing a "late" value with an "earlier" value produces a positive number. In this case, the result is (365 * 5) + 1, because one of the intervening years is a leap year.

select now() as right_now, datediff(now() + interval 5 years, now()) as in_5_years;
+-------------------------------+------------+
| right_now                     | in_5_years |
+-------------------------------+------------+
| 2016-05-20 13:43:55.873826000 | 1826       |
+-------------------------------+------------+

The following examples show how the return value represent the number of days between the associated dates, regardless of the time portion of each TIMESTAMP. For example, different times on the same day produce a DATE_DIFF() of 0, regardless of which one is earlier or later. But if the arguments represent different dates, DATE_DIFF() returns a non-zero integer value, regardless of the time portions of the dates.

select now() as right_now, datediff(now(), now() + interval 4 hours) as in_4_hours;
+-------------------------------+------------+
| right_now                     | in_4_hours |
+-------------------------------+------------+
| 2016-05-20 13:42:05.302747000 | 0          |
+-------------------------------+------------+

select now() as right_now, datediff(now(), now() - interval 4 hours) as 4_hours_ago;
+-------------------------------+-------------+
| right_now                     | 4_hours_ago |
+-------------------------------+-------------+
| 2016-05-20 13:42:21.134958000 | 0           |
+-------------------------------+-------------+

select now() as right_now, datediff(now(), now() + interval 12 hours) as in_12_hours;
+-------------------------------+-------------+
| right_now                     | in_12_hours |
+-------------------------------+-------------+
| 2016-05-20 13:42:44.765873000 | -1          |
+-------------------------------+-------------+

select now() as right_now, datediff(now(), now() - interval 18 hours) as 18_hours_ago;
+-------------------------------+--------------+
| right_now                     | 18_hours_ago |
+-------------------------------+--------------+
| 2016-05-20 13:54:38.829827000 | 1            |
+-------------------------------+--------------+
DAY(TIMESTAMP date), DAYOFMONTH(TIMESTAMP date)
Purpose: Returns the day field from the date portion of a TIMESTAMP. The value represents the day of the month, therefore is in the range 1-31, or less for months without 31 days.

Return type: INT

Examples:

The following examples show how the day value corresponds to the day of the month, resetting back to 1 at the start of each month.

select now(), day(now());
+-------------------------------+------------+
| now()                         | day(now()) |
+-------------------------------+------------+
| 2016-05-20 15:01:51.042185000 | 20         |
+-------------------------------+------------+

select now() + interval 11 days, day(now() + interval 11 days);
+-------------------------------+-------------------------------+
| now() + interval 11 days      | day(now() + interval 11 days) |
+-------------------------------+-------------------------------+
| 2016-05-31 15:05:56.843139000 | 31                            |
+-------------------------------+-------------------------------+

select now() + interval 12 days, day(now() + interval 12 days);
+-------------------------------+-------------------------------+
| now() + interval 12 days      | day(now() + interval 12 days) |
+-------------------------------+-------------------------------+
| 2016-06-01 15:06:05.074236000 | 1                             |
+-------------------------------+-------------------------------+

The following examples show how the day value is NULL for nonexistent dates or misformatted date strings.

-- 2016 is a leap year, so it has a Feb. 29.
select day('2016-02-29');
+-------------------+
| day('2016-02-29') |
+-------------------+
| 29                |
+-------------------+

-- 2015 is not a leap year, so Feb. 29 is nonexistent.
select day('2015-02-29');
+-------------------+
| day('2015-02-29') |
+-------------------+
| NULL              |
+-------------------+

-- A string that does not match the expected YYYY-MM-DD format
-- produces an invalid TIMESTAMP, causing day() to return NULL.
select day('2016-02-028');
+--------------------+
| day('2016-02-028') |
+--------------------+
| NULL               |
+--------------------+
DAYNAME(TIMESTAMP date)
Purpose: Returns the day field from a TIMESTAMP value, converted to the string corresponding to that day name. The range of return values is 'Sunday' to 'Saturday'. Used in report-generating queries, as an alternative to calling DAYOFWEEK() and turning that numeric return value into a string using a CASE expression.

Return type: STRING

Examples:

The following examples show the day name associated with TIMESTAMP values representing different days.

select now() as right_now,
  dayofweek(now()) as todays_day_of_week,
  dayname(now()) as todays_day_name;
+-------------------------------+--------------------+-----------------+
| right_now                     | todays_day_of_week | todays_day_name |
+-------------------------------+--------------------+-----------------+
| 2016-05-31 10:57:03.953670000 | 3                  | Tuesday         |
+-------------------------------+--------------------+-----------------+

select now() + interval 1 day as tomorrow,
  dayname(now() + interval 1 day) as tomorrows_day_name;
+-------------------------------+--------------------+
| tomorrow                      | tomorrows_day_name |
+-------------------------------+--------------------+
| 2016-06-01 10:58:53.945761000 | Wednesday          |
+-------------------------------+--------------------+
DAYOFWEEK(TIMESTAMP date)
Purpose: Returns the day field from the date portion of a TIMESTAMP, corresponding to the day of the week. The range of return values is 1 (Sunday) to 7 (Saturday).

Return type: INT

Examples:

select now() as right_now,
  dayofweek(now()) as todays_day_of_week,
  dayname(now()) as todays_day_name;
+-------------------------------+--------------------+-----------------+
| right_now                     | todays_day_of_week | todays_day_name |
+-------------------------------+--------------------+-----------------+
| 2016-05-31 10:57:03.953670000 | 3                  | Tuesday         |
+-------------------------------+--------------------+-----------------+
DAYOFYEAR(TIMESTAMP date)
Purpose: Returns the day field from a TIMESTAMP value, corresponding to the day of the year. The range of return values is 1 (January 1) to 366 (December 31 of a leap year).

Return type: INT

Examples:

The following examples show return values from the dayofyear() function. The same date in different years returns a different day number for all dates after February 28, because 2016 is a leap year while 2015 is not a leap year.

select now() as right_now,
  dayofyear(now()) as today_day_of_year;
+-------------------------------+-------------------+
| right_now                     | today_day_of_year |
+-------------------------------+-------------------+
| 2016-05-31 11:05:48.314932000 | 152               |
+-------------------------------+-------------------+

select now() - interval 1 year as last_year,
  dayofyear(now() - interval 1 year) as year_ago_day_of_year;
+-------------------------------+----------------------+
| last_year                     | year_ago_day_of_year |
+-------------------------------+----------------------+
| 2015-05-31 11:07:03.733689000 | 151                  |
+-------------------------------+----------------------+
DAYS_ADD(TIMESTAMP startdate, INT days), DAYS_ADD(TIMESTAMP startdate, BIGINT days)
Purpose: Adds a specified number of days to a TIMESTAMP value. Similar to DATE_ADD(), but starts with an actual TIMESTAMP value instead of a string that is converted to a TIMESTAMP.

Return type: TIMESTAMP

Examples:

select now() as right_now, days_add(now(), 31) as 31_days_later;
+-------------------------------+-------------------------------+
| right_now                     | 31_days_later                 |
+-------------------------------+-------------------------------+
| 2016-05-31 11:12:32.216764000 | 2016-07-01 11:12:32.216764000 |
+-------------------------------+-------------------------------+
DAYS_SUB(TIMESTAMP startdate, INT days), DAYS_SUB(TIMESTAMP startdate, BIGINT days)
Purpose: Subtracts a specified number of days from a TIMESTAMP value. Similar to DATE_SUB(), but starts with an actual TIMESTAMP value instead of a string that is converted to a TIMESTAMP.

Return type: TIMESTAMP

Examples:

select now() as right_now, days_sub(now(), 31) as 31_days_ago;
+-------------------------------+-------------------------------+
| right_now                     | 31_days_ago                   |
+-------------------------------+-------------------------------+
| 2016-05-31 11:13:42.163905000 | 2016-04-30 11:13:42.163905000 |
+-------------------------------+-------------------------------+
EXTRACT(TIMESTAMP timestamp, STRING unit), EXTRACT(unit FROM TIMESTAMP ts)
Purpose: Returns one of the numeric date or time fields from a TIMESTAMP value.

Unit argument: The unit string can be one of epoch, year, quarter, month, day, hour, minute, second, or millisecond. This argument value is case-insensitive.

If you specify millisecond for the unit argument, the function returns the seconds component and the milliseconds component. For example, EXTRACT(CAST('2006-05-12 18:27:28.123456789' AS TIMESTAMP), 'MILLISECOND') will return 28123.

In Impala 2.0 and higher, you can use special syntax rather than a regular function call, for compatibility with code that uses the SQL-99 format with the FROM keyword. With this style, the unit names are identifiers rather than STRING literals. For example, the following calls are both equivalent:
EXTRACT(year FROM NOW());
EXTRACT(NOW(), 'year');

Usage notes:

Typically used in GROUP BY queries to arrange results by hour, day, month, and so on. You can also use this function in an INSERT ... SELECT statement to insert into a partitioned table to split up TIMESTAMP values into individual parts, if the partitioned table has separate partition key columns representing year, month, day, and so on. If you need to divide by more complex units of time, such as by week or by quarter, use the TRUNC() function instead.

Return type: BIGINT

Examples:

SELECT NOW() AS right_now,
  EXTRACT(day FROM NOW()) AS this_day,
  EXTRACT(hour FROM NOW()) AS this_hour;
+-------------------------------+----------+-----------+
| right_now                     | this_day | this_hour |
+-------------------------------+----------+-----------+
| 2016-05-31 11:19:24.025303000 | 31       | 11        |
+-------------------------------+----------+-----------+
FROM_TIMESTAMP(TIMESTAMP datetime, STRING pattern), FROM_TIMESTAMP(STRING datetime, STRING pattern)
Purpose: Converts a TIMESTAMP value into a string representing the same value.

Return type: STRING

Added in: CDH 5.5.0 / Impala 2.3.0

Usage notes:

The FROM_TIMESTAMP() function provides a flexible way to convert TIMESTAMP values into arbitrary string formats for reporting purposes.

Because Impala implicitly converts string values into TIMESTAMP, you can pass date/time values represented as strings (in the standard yyyy-MM-dd HH:mm:ss.SSS format) to this function. The result is a string using different separator characters, order of fields, spelled-out month names, or other variation of the date/time string representation.

The allowed tokens for the pattern string are the same as for the FROM_UNIXTIME() function.

Examples:

The following examples show different ways to format a TIMESTAMP value as a string:

-- Reformat a TIMESTAMP value.
SELECT FROM_TIMESTAMP(NOW(), 'yyyy/MM/dd');
+-------------------------------------+
| from_timestamp(now(), 'yyyy/mm/dd') |
+-------------------------------------+
| 2018/10/09                          |
+-------------------------------------+

-- Alternative format for reporting purposes.
SELECT FROM_TIMESTAMP('1984-09-25 16:45:30.125', 'MMM dd, yyyy HH:mm:ss.SSS');
+------------------------------------------------------------------------+
| from_timestamp('1984-09-25 16:45:30.125', 'mmm dd, yyyy hh:mm:ss.sss') |
+------------------------------------------------------------------------+
| Sep 25, 1984 16:45:30.125                                              |
+------------------------------------------------------------------------+
FROM_UNIXTIME(BIGINT unixtime[, STRING format])
Purpose: Converts the number of seconds from the Unix epoch to the specified time into a string in the local time zone.

Return type: STRING

In Impala 2.2.0 and higher, built-in functions that accept or return integers representing TIMESTAMP values use the BIGINT type for parameters and return values, rather than INT. This change lets the date and time functions avoid an overflow error that would otherwise occur on January 19th, 2038 (known as the "Year 2038 problem" or "Y2K38 problem"). This change affects the FROM_UNIXTIME() and UNIX_TIMESTAMP() functions. You might need to change application code that interacts with these functions, change the types of columns that store the return values, or add CAST() calls to SQL statements that call these functions.

Usage notes:

The format string accepts the variations allowed for the TIMESTAMP data type: date plus time, date by itself, time by itself, and optional fractional seconds for the time. See TIMESTAMP Data Type for details.

Currently, the format string is case-sensitive, especially to distinguish m for minutes and M for months. In Impala 1.3 and later, you can switch the order of elements, use alternative separator characters, and use a different number of placeholders for each unit. Adding more instances of y, d, H, and so on produces output strings zero-padded to the requested number of characters. The exception is M for months, where M produces a non-padded value such as 3, MM produces a zero-padded value such as 03, MMM produces an abbreviated month name such as Mar, and sequences of 4 or more M are not allowed. A date string including all fields could be 'yyyy-MM-dd HH:mm:ss.SSSSSS', 'dd/MM/yyyy HH:mm:ss.SSSSSS', 'MMM dd, yyyy HH.mm.ss (SSSSSS)' or other combinations of placeholders and separator characters.

The way this function deals with time zones when converting to or from TIMESTAMP values is affected by the ‑‑use_local_tz_for_unix_timestamp_conversions startup flag for the impalad daemon. See TIMESTAMP Data Type for details about how Impala handles time zone considerations for the TIMESTAMP data type.

Examples:

SELECT FROM_UNIXTIME(1392394861,'yyyy-MM-dd HH:mm:ss.SSSS');
+-------------------------------------------------------+
| from_unixtime(1392394861, 'yyyy-mm-dd hh:mm:ss.ssss') |
+-------------------------------------------------------+
| 2014-02-14 16:21:01.0000                              |
+-------------------------------------------------------+

SELECT FROM_UNIXTIME(1392394861,'HH:mm:ss.SSSS');
+--------------------------------------------+
| from_unixtime(1392394861, 'hh:mm:ss.ssss') |
+--------------------------------------------+
| 16:21:01.0000                              |
+--------------------------------------------+
UNIX_TIMESTAMP() and FROM_UNIXTIME() are often used in combination to convert a TIMESTAMP value into a particular string format. For example:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(NOW() + interval 3 days),
  'yyyy/MM/dd HH:mm') AS yyyy_mm_dd_hh_mm;
+------------------+
| yyyy_mm_dd_hh_mm |
+------------------+
| 2016/06/03 11:38 |
+------------------+
FROM_UTC_TIMESTAMP(TIMESTAMP timestamp, STRING timezone)
Purpose: Converts a specified UTC timestamp value into the appropriate value for a specified time zone.

Return type: TIMESTAMP

Usage notes: Often used to translate UTC time zone data stored in a table back to the local date and time for reporting. The opposite of the TO_UTC_TIMESTAMP() function.

To determine the time zone of the server you are connected to, in CDH 5.5 / Impala 2.3 and higher you can call the timeofday() function, which includes the time zone specifier in its return value. Remember that with cloud computing, the server you interact with might be in a different time zone than you are, or different sessions might connect to servers in different time zones, or a cluster might include servers in more than one time zone.

Examples:

See discussion of time zones in TIMESTAMP Data Type for information about using this function for conversions between the local time zone and UTC.

The following example shows how when TIMESTAMP values representing the UTC time zone are stored in a table, a query can display the equivalent local date and time for a different time zone.

with t1 as (select cast('2016-06-02 16:25:36.116143000' as timestamp) as utc_datetime)
  select utc_datetime as 'Date/time in Greenwich UK',
    from_utc_timestamp(utc_datetime, 'PDT')
      as 'Equivalent in California USA'
  from t1;
+-------------------------------+-------------------------------+
| date/time in greenwich uk     | equivalent in california usa  |
+-------------------------------+-------------------------------+
| 2016-06-02 16:25:36.116143000 | 2016-06-02 09:25:36.116143000 |
+-------------------------------+-------------------------------+

The following example shows that for a date and time when daylight savings is in effect (PDT), the UTC time is 7 hours ahead of the local California time; while when daylight savings is not in effect (PST), the UTC time is 8 hours ahead of the local California time.

select now() as local_datetime,
  to_utc_timestamp(now(), 'PDT') as utc_datetime;
+-------------------------------+-------------------------------+
| local_datetime                | utc_datetime                  |
+-------------------------------+-------------------------------+
| 2016-05-31 11:50:02.316883000 | 2016-05-31 18:50:02.316883000 |
+-------------------------------+-------------------------------+

select '2016-01-05' as local_datetime,
  to_utc_timestamp('2016-01-05', 'PST') as utc_datetime;
+----------------+---------------------+
| local_datetime | utc_datetime        |
+----------------+---------------------+
| 2016-01-05     | 2016-01-05 08:00:00 |
+----------------+---------------------+
HOUR(TIMESTAMP date)
Purpose: Returns the hour field from a TIMESTAMP field.

Return type: INT

Examples:

select now() as right_now, hour(now()) as current_hour;
+-------------------------------+--------------+
| right_now                     | current_hour |
+-------------------------------+--------------+
| 2016-06-01 14:14:12.472846000 | 14           |
+-------------------------------+--------------+

select now() + interval 12 hours as 12_hours_from_now,
  hour(now() + interval 12 hours) as hour_in_12_hours;
+-------------------------------+-------------------+
| 12_hours_from_now             | hour_in_12_hours  |
+-------------------------------+-------------------+
| 2016-06-02 02:15:32.454750000 | 2                 |
+-------------------------------+-------------------+
HOURS_ADD(TIMESTAMP date, INT hours), HOURS_ADD(TIMESTAMP date, BIGINT hours)
Purpose: Returns the specified date and time plus some number of hours.

Return type: TIMESTAMP

Examples:

select now() as right_now,
  hours_add(now(), 12) as in_12_hours;
+-------------------------------+-------------------------------+
| right_now                     | in_12_hours                   |
+-------------------------------+-------------------------------+
| 2016-06-01 14:19:48.948107000 | 2016-06-02 02:19:48.948107000 |
+-------------------------------+-------------------------------+
HOURS_SUB(TIMESTAMP date, INT hours), HOURS_SUB(TIMESTAMP date, BIGINT hours)
Purpose: Returns the specified date and time minus some number of hours.

Return type: TIMESTAMP

Examples:

select now() as right_now,
  hours_sub(now(), 18) as 18_hours_ago;
+-------------------------------+-------------------------------+
| right_now                     | 18_hours_ago                  |
+-------------------------------+-------------------------------+
| 2016-06-01 14:23:13.868150000 | 2016-05-31 20:23:13.868150000 |
+-------------------------------+-------------------------------+
INT_MONTHS_BETWEEN(TIMESTAMP newer, TIMESTAMP older)
Purpose: Returns the number of months between the date portions of two TIMESTAMP values, as an INT representing only the full months that passed.

Return type: INT

Added in: CDH 5.5.0 / Impala 2.3.0

Usage notes:

Typically used in business contexts, for example to determine whether a specified number of months have passed or whether some end-of-month deadline was reached.

The method of determining the number of elapsed months includes some special handling of months with different numbers of days that creates edge cases for dates between the 28th and 31st days of certain months. See MONTHS_BETWEEN() for details. The INT_MOINTHS_BETWEEN() result is essentially the FLOOR() of the MONTHS_BETWEEN() result.

If either value is NULL, which could happen for example when converting a nonexistent date string such as '2015-02-29' to a TIMESTAMP, the result is also NULL.

If the first argument represents an earlier time than the second argument, the result is negative.

Examples:

/* Less than a full month = 0. */
select int_months_between('2015-02-28', '2015-01-29');
+------------------------------------------------+
| int_months_between('2015-02-28', '2015-01-29') |
+------------------------------------------------+
| 0                                              |
+------------------------------------------------+

/* Last day of month to last day of next month = 1. */
select int_months_between('2015-02-28', '2015-01-31');
+------------------------------------------------+
| int_months_between('2015-02-28', '2015-01-31') |
+------------------------------------------------+
| 1                                              |
+------------------------------------------------+

/* Slightly less than 2 months = 1. */
select int_months_between('2015-03-28', '2015-01-31');
+------------------------------------------------+
| int_months_between('2015-03-28', '2015-01-31') |
+------------------------------------------------+
| 1                                              |
+------------------------------------------------+

/* 2 full months (identical days of the month) = 2. */
select int_months_between('2015-03-31', '2015-01-31');
+------------------------------------------------+
| int_months_between('2015-03-31', '2015-01-31') |
+------------------------------------------------+
| 2                                              |
+------------------------------------------------+

/* Last day of month to last day of month-after-next = 2. */
select int_months_between('2015-03-31', '2015-01-30');
+------------------------------------------------+
| int_months_between('2015-03-31', '2015-01-30') |
+------------------------------------------------+
| 2                                              |
+------------------------------------------------+
LAST_DAY(TIMESTAMP t)
Purpose: Returns a TIMESTAMP corresponding to the beginning of the last calendar day in the same month as the TIMESTAMP argument.

Return type: TIMESTAMP

Added in: CDH 5.12.0 / Impala 2.9.0

Usage notes:

If the input argument does not represent a valid Impala TIMESTAMP including both date and time portions, the function returns NULL. For example, if the input argument is a string that cannot be implicitly cast to TIMESTAMP, does not include a date portion, or is out of the allowed range for Impala TIMESTAMP values, the function returns NULL.

Examples:

The following example shows how to examine the current date, and dates around the end of the month, as TIMESTAMP values with any time portion removed:

select
    now() as right_now
  , trunc(now(),'dd') as today
  , last_day(now()) as last_day_of_month
  , last_day(now()) + interval 1 day as first_of_next_month;
+-------------------------------+---------------------+---------------------+---------------------+
| right_now                     | today               | last_day_of_month   | first_of_next_month |
+-------------------------------+---------------------+---------------------+---------------------+
| 2017-08-15 15:07:58.823812000 | 2017-08-15 00:00:00 | 2017-08-31 00:00:00 | 2017-09-01 00:00:00 |
+-------------------------------+---------------------+---------------------+---------------------+

The following example shows how to examine the current date and dates around the end of the month as integers representing the day of the month:

select
    now() as right_now
  , dayofmonth(now()) as day
  , extract(day from now()) as also_day
  , dayofmonth(last_day(now())) as last_day
  , extract(day from last_day(now())) as also_last_day;
+-------------------------------+-----+----------+----------+---------------+
| right_now                     | day | also_day | last_day | also_last_day |
+-------------------------------+-----+----------+----------+---------------+
| 2017-08-15 15:07:59.417755000 | 15  | 15       | 31       | 31            |
+-------------------------------+-----+----------+----------+---------------+
MICROSECONDS_ADD(TIMESTAMP date, INT microseconds), MICROSECONDS_ADD(TIMESTAMP date, BIGINT microseconds)
Purpose: Returns the specified date and time plus some number of microseconds.

Return type: TIMESTAMP

Examples:

select now() as right_now,
  microseconds_add(now(), 500000) as half_a_second_from_now;
+-------------------------------+-------------------------------+
| right_now                     | half_a_second_from_now        |
+-------------------------------+-------------------------------+
| 2016-06-01 14:25:11.455051000 | 2016-06-01 14:25:11.955051000 |
+-------------------------------+-------------------------------+
MICROSECONDS_SUB(TIMESTAMP date, INT microseconds), MICROSECONDS_SUB(TIMESTAMP date, BIGINT microseconds)
Purpose: Returns the specified date and time minus some number of microseconds.

Return type: TIMESTAMP

Examples:

select now() as right_now,
  microseconds_sub(now(), 500000) as half_a_second_ago;
+-------------------------------+-------------------------------+
| right_now                     | half_a_second_ago             |
+-------------------------------+-------------------------------+
| 2016-06-01 14:26:16.509990000 | 2016-06-01 14:26:16.009990000 |
+-------------------------------+-------------------------------+
MILLISECOND(TIMESTAMP t)
Purpose: Returns the millisecond portion of a t value.

Return type: INT

Added in: CDH 5.7.0 / Impala 2.5.0

Usage notes:

The millisecond value is truncated, not rounded, if the TIMESTAMP value contains more than 3 significant digits to the right of the decimal point.

Examples:

252.4 milliseconds truncated to 252.

select now(), millisecond(now());
+-------------------------------+--------------------+
| now()                         | millisecond(now()) |
+-------------------------------+--------------------+
| 2016-03-14 22:30:25.252400000 | 252                |
+-------------------------------+--------------------+

761.767 milliseconds truncated to 761.

select now(), millisecond(now());
+-------------------------------+--------------------+
| now()                         | millisecond(now()) |
+-------------------------------+--------------------+
| 2016-03-14 22:30:58.761767000 | 761                |
+-------------------------------+--------------------+
MILLISECONDS_ADD(TIMESTAMP date, INT milliseconds), MILLISECONDS_ADD(TIMESTAMP date, BIGINT milliseconds)
Purpose: Returns the specified date and time plus some number of milliseconds.

Return type: TIMESTAMP

Examples:

select now() as right_now,
  milliseconds_add(now(), 1500) as 1_point_5_seconds_from_now;
+-------------------------------+-------------------------------+
| right_now                     | 1_point_5_seconds_from_now    |
+-------------------------------+-------------------------------+
| 2016-06-01 14:30:30.067366000 | 2016-06-01 14:30:31.567366000 |
+-------------------------------+-------------------------------+
MILLISECONDS_SUB(TIMESTAMP date, INT milliseconds), MILLISECONDS_SUB(TIMESTAMP date, BIGINT milliseconds)
Purpose: Returns the specified date and time minus some number of milliseconds.

Return type: TIMESTAMP

Examples:

select now() as right_now,
  milliseconds_sub(now(), 1500) as 1_point_5_seconds_ago;
+-------------------------------+-------------------------------+
| right_now                     | 1_point_5_seconds_ago         |
+-------------------------------+-------------------------------+
| 2016-06-01 14:30:53.467140000 | 2016-06-01 14:30:51.967140000 |
+-------------------------------+-------------------------------+
MINUTE(TIMESTAMP date)
Purpose: Returns the minute field from a TIMESTAMP value.

Return type: INT

Examples:

select now() as right_now, minute(now()) as current_minute;
+-------------------------------+----------------+
| right_now                     | current_minute |
+-------------------------------+----------------+
| 2016-06-01 14:34:08.051702000 | 34             |
+-------------------------------+----------------+
MINUTES_ADD(TIMESTAMP date, INT minutes), MINUTES_ADD(TIMESTAMP date, BIGINT minutes)
Purpose: Returns the specified date and time plus some number of minutes.

Return type: TIMESTAMP

Examples:

select now() as right_now, minutes_add(now(), 90) as 90_minutes_from_now;
+-------------------------------+-------------------------------+
| right_now                     | 90_minutes_from_now           |
+-------------------------------+-------------------------------+
| 2016-06-01 14:36:04.887095000 | 2016-06-01 16:06:04.887095000 |
+-------------------------------+-------------------------------+
MINUTES_SUB(TIMESTAMP date, INT minutes), MINUTES_SUB(TIMESTAMP date, BIGINT minutes)
Purpose: Returns the specified date and time minus some number of minutes.

Return type: TIMESTAMP

Examples:

select now() as right_now, minutes_sub(now(), 90) as 90_minutes_ago;
+-------------------------------+-------------------------------+
| right_now                     | 90_minutes_ago                |
+-------------------------------+-------------------------------+
| 2016-06-01 14:36:32.643061000 | 2016-06-01 13:06:32.643061000 |
+-------------------------------+-------------------------------+
MONTH(TIMESTAMP date)
Purpose: Returns the month field, represented as an integer, from the date portion of a TIMESTAMP.

Return type: INT

Examples:

select now() as right_now, month(now()) as current_month;
+-------------------------------+---------------+
| right_now                     | current_month |
+-------------------------------+---------------+
| 2016-06-01 14:43:37.141542000 | 6             |
+-------------------------------+---------------+
MONTHNAME(TIMESTAMP date)
Purpose: Returns the month field from a TIMESTAMP value, converted to the string corresponding to that month name.

Return type: STRING

MONTHS_ADD(TIMESTAMP date, INT months), MONTHS_ADD(TIMESTAMP date, BIGINT months)
Purpose: Returns the specified date and time plus some number of months.

Return type: TIMESTAMP

Examples:

The following example shows the effects of adding some number of months to a TIMESTAMP value, using both the months_add() function and its add_months() alias. These examples use trunc() to strip off the time portion and leave just the date.

with t1 as (select trunc(now(), 'dd') as today)
  select today, months_add(today,1) as next_month from t1;
+---------------------+---------------------+
| today               | next_month          |
+---------------------+---------------------+
| 2016-05-19 00:00:00 | 2016-06-19 00:00:00 |
+---------------------+---------------------+

with t1 as (select trunc(now(), 'dd') as today)
  select today, add_months(today,1) as next_month from t1;
+---------------------+---------------------+
| today               | next_month          |
+---------------------+---------------------+
| 2016-05-19 00:00:00 | 2016-06-19 00:00:00 |
+---------------------+---------------------+

The following examples show how if months_add() would return a nonexistent date, due to different months having different numbers of days, the function returns a TIMESTAMP from the last day of the relevant month. For example, adding one month to January 31 produces a date of February 29th in the year 2016 (a leap year), and February 28th in the year 2015 (a non-leap year).

with t1 as (select cast('2016-01-31' as timestamp) as jan_31)
  select jan_31, months_add(jan_31,1) as feb_31 from t1;
+---------------------+---------------------+
| jan_31              | feb_31              |
+---------------------+---------------------+
| 2016-01-31 00:00:00 | 2016-02-29 00:00:00 |
+---------------------+---------------------+

with t1 as (select cast('2015-01-31' as timestamp) as jan_31)
  select jan_31, months_add(jan_31,1) as feb_31 from t1;
+---------------------+---------------------+
| jan_31              | feb_31              |
+---------------------+---------------------+
| 2015-01-31 00:00:00 | 2015-02-28 00:00:00 |
+---------------------+---------------------+
MONTHS_BETWEEN(TIMESTAMP newer, TIMESTAMP older)
Purpose: Returns the number of months between the date portions of two TIMESTAMP values. Can include a fractional part representing extra days in addition to the full months between the dates. The fractional component is computed by dividing the difference in days by 31 (regardless of the month).

Return type: DOUBLE

Added in: CDH 5.5.0 / Impala 2.3.0

Usage notes:

Typically used in business contexts, for example to determine whether a specified number of months have passed or whether some end-of-month deadline was reached.

If the only consideration is the number of full months and any fractional value is not significant, use int_months_between() instead.

The method of determining the number of elapsed months includes some special handling of months with different numbers of days that creates edge cases for dates between the 28th and 31st days of certain months.

If either value is NULL, which could happen for example when converting a nonexistent date string such as '2015-02-29' to a TIMESTAMP, the result is also NULL.

If the first argument represents an earlier time than the second argument, the result is negative.

Examples:

The following examples show how dates that are on the same day of the month are considered to be exactly N months apart, even if the months have different numbers of days.

select months_between('2015-02-28', '2015-01-28');
+--------------------------------------------+
| months_between('2015-02-28', '2015-01-28') |
+--------------------------------------------+
| 1                                          |
+--------------------------------------------+

select months_between(now(), now() + interval 1 month);
+-------------------------------------------------+
| months_between(now(), now() + interval 1 month) |
+-------------------------------------------------+
| -1                                              |
+-------------------------------------------------+

select months_between(now() + interval 1 year, now());
+------------------------------------------------+
| months_between(now() + interval 1 year, now()) |
+------------------------------------------------+
| 12                                             |
+------------------------------------------------+

The following examples show how dates that are on the last day of the month are considered to be exactly N months apart, even if the months have different numbers of days. For example, from January 28th to February 28th is exactly one month because the day of the month is identical; January 31st to February 28th is exactly one month because in both cases it is the last day of the month; but January 29th or 30th to February 28th is considered a fractional month.

select months_between('2015-02-28', '2015-01-31');
+--------------------------------------------+
| months_between('2015-02-28', '2015-01-31') |
+--------------------------------------------+
| 1                                          |
+--------------------------------------------+

select months_between('2015-02-28', '2015-01-29');
+--------------------------------------------+
| months_between('2015-02-28', '2015-01-29') |
+--------------------------------------------+
| 0.967741935483871                          |
+--------------------------------------------+

select months_between('2015-02-28', '2015-01-30');;
+--------------------------------------------+
| months_between('2015-02-28', '2015-01-30') |
+--------------------------------------------+
| 0.935483870967742                          |
+--------------------------------------------+

The following examples show how dates that are not a precise number of months apart result in a fractional return value.

select months_between('2015-03-01', '2015-01-28');
+--------------------------------------------+
| months_between('2015-03-01', '2015-01-28') |
+--------------------------------------------+
| 1.129032258064516                          |
+--------------------------------------------+

select months_between('2015-03-01', '2015-02-28');
+--------------------------------------------+
| months_between('2015-03-01', '2015-02-28') |
+--------------------------------------------+
| 0.1290322580645161                         |
+--------------------------------------------+

select months_between('2015-06-02', '2015-05-29');
+--------------------------------------------+
| months_between('2015-06-02', '2015-05-29') |
+--------------------------------------------+
| 0.1290322580645161                         |
+--------------------------------------------+

select months_between('2015-03-01', '2015-01-25');
+--------------------------------------------+
| months_between('2015-03-01', '2015-01-25') |
+--------------------------------------------+
| 1.225806451612903                          |
+--------------------------------------------+

select months_between('2015-03-01', '2015-02-25');
+--------------------------------------------+
| months_between('2015-03-01', '2015-02-25') |
+--------------------------------------------+
| 0.2258064516129032                         |
+--------------------------------------------+

select months_between('2015-02-28', '2015-02-01');
+--------------------------------------------+
| months_between('2015-02-28', '2015-02-01') |
+--------------------------------------------+
| 0.8709677419354839                         |
+--------------------------------------------+

select months_between('2015-03-28', '2015-03-01');
+--------------------------------------------+
| months_between('2015-03-28', '2015-03-01') |
+--------------------------------------------+
| 0.8709677419354839                         |
+--------------------------------------------+

The following examples show how the time portion of the TIMESTAMP values are irrelevant for calculating the month interval. Even the fractional part of the result only depends on the number of full days between the argument values, regardless of the time portion.

select months_between('2015-05-28 23:00:00', '2015-04-28 11:45:00');
+--------------------------------------------------------------+
| months_between('2015-05-28 23:00:00', '2015-04-28 11:45:00') |
+--------------------------------------------------------------+
| 1                                                            |
+--------------------------------------------------------------+

select months_between('2015-03-28', '2015-03-01');
+--------------------------------------------+
| months_between('2015-03-28', '2015-03-01') |
+--------------------------------------------+
| 0.8709677419354839                         |
+--------------------------------------------+

select months_between('2015-03-28 23:00:00', '2015-03-01 11:45:00');
+--------------------------------------------------------------+
| months_between('2015-03-28 23:00:00', '2015-03-01 11:45:00') |
+--------------------------------------------------------------+
| 0.8709677419354839                                           |
+--------------------------------------------------------------+
MONTHS_SUB(TIMESTAMP date, INT months), MONTHS_SUB(TIMESTAMP date, BIGINT months)
Purpose: Returns the specified date and time minus some number of months.

Return type: TIMESTAMP

Examples:

with t1 as (select trunc(now(), 'dd') as today)
  select today, months_sub(today,1) as last_month from t1;
+---------------------+---------------------+
| today               | last_month          |
+---------------------+---------------------+
| 2016-06-01 00:00:00 | 2016-05-01 00:00:00 |
+---------------------+---------------------+
NANOSECONDS_ADD(TIMESTAMP date, INT nanoseconds), NANOSECONDS_ADD(TIMESTAMP date, BIGINT nanoseconds)
Purpose: Returns the specified date and time plus some number of nanoseconds.

Return type: TIMESTAMP

Kudu considerations:

The nanosecond portion of an Impala TIMESTAMP value is rounded to the nearest microsecond when that value is stored in a Kudu table.

Examples:

select now() as right_now, nanoseconds_add(now(), 1) as 1_nanosecond_later;
+-------------------------------+-------------------------------+
| right_now                     | 1_nanosecond_later            |
+-------------------------------+-------------------------------+
| 2016-06-01 15:42:00.361026000 | 2016-06-01 15:42:00.361026001 |
+-------------------------------+-------------------------------+

-- 1 billion nanoseconds = 1 second.
select now() as right_now, nanoseconds_add(now(), 1e9) as 1_second_later;
+-------------------------------+-------------------------------+
| right_now                     | 1_second_later                |
+-------------------------------+-------------------------------+
| 2016-06-01 15:42:52.926706000 | 2016-06-01 15:42:53.926706000 |
+-------------------------------+-------------------------------+
NANOSECONDS_SUB(TIMESTAMP date, INT nanoseconds), NANOSECONDS_SUB(TIMESTAMP date, BIGINT nanoseconds)
Purpose: Returns the specified date and time minus some number of nanoseconds.

Return type: TIMESTAMP

Kudu considerations:

The nanosecond portion of an Impala TIMESTAMP value is rounded to the nearest microsecond when that value is stored in a Kudu table.

select now() as right_now, nanoseconds_sub(now(), 1) as 1_nanosecond_earlier;
+-------------------------------+-------------------------------+
| right_now                     | 1_nanosecond_earlier          |
+-------------------------------+-------------------------------+
| 2016-06-01 15:44:14.355837000 | 2016-06-01 15:44:14.355836999 |
+-------------------------------+-------------------------------+

-- 1 billion nanoseconds = 1 second.
select now() as right_now, nanoseconds_sub(now(), 1e9) as 1_second_earlier;
+-------------------------------+-------------------------------+
| right_now                     | 1_second_earlier              |
+-------------------------------+-------------------------------+
| 2016-06-01 15:44:54.474929000 | 2016-06-01 15:44:53.474929000 |
+-------------------------------+-------------------------------+
NEXT_DAY(TIMESTAMP date, STRING weekday)
Purpose: Returns the date of the weekday that follows the specified date.

Return type: TIMESTAMP

Usage notes:

The weekday parameter is case-insensitive. The following values are accepted for weekday: "Sunday"/"Sun", "Monday"/"Mon", "Tuesday"/"Tue", "Wednesday"/"Wed", "Thursday"/"Thu", "Friday"/"Fri", "Saturday"/"Sat"

Calling the function with the current date and weekday returns the date that is one week later.

Examples:

select next_day('2013-12-25','Saturday');
-- Returns '2013-12-28 00:00:00' the first Saturday after December 25, 2013.
select next_day(to_timestamp('08-1987-21', 'mm-yyyy-dd'), 'Friday');
-- Returns '1987-08-28 00:00:00' the first Friday after August 28, 1987.

select next_day(now(), 'Thu');
-- Executed on 2018-07-12, the function returns '2018-07-13 00:00:00', one week
-- after the current date.
NOW()
Purpose: Returns the current date and time (in the local time zone) as a TIMESTAMP value.

Return type: TIMESTAMP

Usage notes:

To find a date/time value in the future or the past relative to the current date and time, add or subtract an INTERVAL expression to the return value of now(). See TIMESTAMP Data Type for examples.

To produce a TIMESTAMP representing the current date and time that can be shared or stored without interoperability problems due to time zone differences, use the to_utc_timestamp() function and specify the time zone of the server. When TIMESTAMP data is stored in UTC form, any application that queries those values can convert them to the appropriate local time zone by calling the inverse function, from_utc_timestamp().

To determine the time zone of the server you are connected to, in CDH 5.5 / Impala 2.3 and higher you can call the timeofday() function, which includes the time zone specifier in its return value. Remember that with cloud computing, the server you interact with might be in a different time zone than you are, or different sessions might connect to servers in different time zones, or a cluster might include servers in more than one time zone.

Any references to the now() function are evaluated at the start of a query. All calls to now() within the same query return the same value, and the value does not depend on how long the query takes.

Examples:

select now() as 'Current time in California USA',
  to_utc_timestamp(now(), 'PDT') as 'Current time in Greenwich UK';
+--------------------------------+-------------------------------+
| current time in california usa | current time in greenwich uk  |
+--------------------------------+-------------------------------+
| 2016-06-01 15:52:08.980072000  | 2016-06-01 22:52:08.980072000 |
+--------------------------------+-------------------------------+

select now() as right_now,
  now() + interval 1 day as tomorrow,
  now() + interval 1 week - interval 3 hours as almost_a_week_from_now;
+-------------------------------+-------------------------------+-------------------------------+
| right_now                     | tomorrow                      | almost_a_week_from_now        |
+-------------------------------+-------------------------------+-------------------------------+
| 2016-06-01 15:55:39.671690000 | 2016-06-02 15:55:39.671690000 | 2016-06-08 12:55:39.671690000 |
+-------------------------------+-------------------------------+-------------------------------+
QUARTER(TIMESTAMP date)
Purpose: Returns the quarter in the input TIMESTAMP expression as an integer value, 1, 2, 3, or 4, where 1 represents January 1 through March 31.

Return type: INT

SECOND(TIMESTAMP date)
Purpose: Returns the second field from a TIMESTAMP value.

Return type: INT

Examples:

select now() as right_now,
  second(now()) as seconds_in_current_minute;
+-------------------------------+---------------------------+
| right_now                     | seconds_in_current_minute |
+-------------------------------+---------------------------+
| 2016-06-01 16:03:57.006603000 | 57                        |
+-------------------------------+---------------------------+
SECONDS_ADD(TIMESTAMP date, INT seconds), SECONDS_ADD(TIMESTAMP date, BIGINT seconds)
Purpose: Returns the specified date and time plus some number of seconds.

Return type: TIMESTAMP

Examples:

select now() as right_now,
  seconds_add(now(), 10) as 10_seconds_from_now;
+-------------------------------+-------------------------------+
| right_now                     | 10_seconds_from_now           |
+-------------------------------+-------------------------------+
| 2016-06-01 16:05:21.573935000 | 2016-06-01 16:05:31.573935000 |
+-------------------------------+-------------------------------+
SECONDS_SUB(TIMESTAMP date, INT seconds), SECONDS_SUB(TIMESTAMP date, BIGINT seconds)
Purpose: Returns the specified date and time minus some number of seconds.

Return type: TIMESTAMP

Examples:

select now() as right_now,
  seconds_sub(now(), 10) as 10_seconds_ago;
+-------------------------------+-------------------------------+
| right_now                     | 10_seconds_ago                |
+-------------------------------+-------------------------------+
| 2016-06-01 16:06:03.467931000 | 2016-06-01 16:05:53.467931000 |
+-------------------------------+-------------------------------+
SUBDATE(TIMESTAMP startdate, INT days), SUBDATE(TIMESTAMP startdate, BIGINT days)
Purpose: Subtracts a specified number of days from a TIMESTAMP value. Similar to DATE_SUB(), but starts with an actual TIMESTAMP value instead of a string that is converted to a TIMESTAMP.

Return type: TIMESTAMP

Examples:

The following examples show how to subtract a number of days from a TIMESTAMP. The number of days can also be negative, which gives the same effect as the ADDDATE() function.

select now() as right_now, subdate(now(), 30) as now_minus_30;
+-------------------------------+-------------------------------+
| right_now                     | now_minus_30                  |
+-------------------------------+-------------------------------+
| 2016-05-20 11:00:15.084991000 | 2016-04-20 11:00:15.084991000 |
+-------------------------------+-------------------------------+

select now() as right_now, subdate(now(), -15) as now_plus_15;
+-------------------------------+-------------------------------+
| right_now                     | now_plus_15                   |
+-------------------------------+-------------------------------+
| 2016-05-20 11:00:44.766091000 | 2016-06-04 11:00:44.766091000 |
+-------------------------------+-------------------------------+
TIMEOFDAY()
Purpose: Returns a string representation of the current date and time, according to the time of the local system, including any time zone designation.

Return type: STRING

Added in: CDH 5.5.0 / Impala 2.3.0

Usage notes: The result value represents similar information as the now() function, only as a STRING type and with somewhat different formatting. For example, the day of the week and the time zone identifier are included. This function is intended primarily for compatibility with SQL code from other systems that also have a timeofday() function. Prefer to use now() if practical for any new Impala code.

Examples:

The following examples show the format of the TIMEOFDAY() return value, illustrate how that value is represented as a STRING that you can manipulate with string processing functions, and how the format compares with the return value from the NOW() function.

/* Date and time fields in a STRING return value. */
select timeofday();
+------------------------------+
| timeofday()                  |
+------------------------------+
| Tue Sep 01 15:13:18 2015 PDT |
+------------------------------+

/* The return value can be processed by other string functions. */
select upper(timeofday());
+------------------------------+
| upper(timeofday())           |
+------------------------------+
| TUE SEP 01 15:13:38 2015 PDT |
+------------------------------+

/* The TIMEOFDAY() result is formatted differently than NOW(). NOW() returns a TIMESTAMP. */
select now(), timeofday();
+-------------------------------+------------------------------+
| now()                         | timeofday()                  |
+-------------------------------+------------------------------+
| 2015-09-01 15:15:25.930021000 | Tue Sep 01 15:15:25 2015 PDT |
+-------------------------------+------------------------------+

/* You can strip out the time zone field to use in calls to from_utc_timestamp(). */
select regexp_replace(timeofday(), '.* ([A-Z]+)$', '\\1') as current_timezone;
+------------------+
| current_timezone |
+------------------+
| PDT              |
+------------------+
TIMESTAMP_CMP(TIMESTAMP t1, TIMESTAMP t2)
Purpose: Tests if one TIMESTAMP value is newer than, older than, or identical to another TIMESTAMP

Return type: INT (either -1, 0, 1, or NULL)

Added in: CDH 5.5.0 / Impala 2.3.0

Usage notes:

Usage notes: A comparison function for TIMESTAMP values that only tests whether the date and time increases, decreases, or stays the same. Similar to the sIGN() function for numeric values.

Examples:

The following examples show all the possible return values for timestamp_cmp(). If the first argument represents a later point in time than the second argument, the result is 1. The amount of the difference is irrelevant, only the fact that one argument is greater than or less than the other. If the first argument represents an earlier point in time than the second argument, the result is -1. If the first and second arguments represent identical points in time, the result is 0. If either argument is NULL, the result is NULL.

/* First argument 'later' than second argument. */

select timestamp_cmp(now() + interval 70 minutes, now())
  as now_vs_in_70_minutes;
+----------------------+
| now_vs_in_70_minutes |
+----------------------+
| 1                    |
+----------------------+

select timestamp_cmp(now() +
  interval 3 days +
  interval 5 hours, now())
  as now_vs_days_from_now;
+----------------------+
| now_vs_days_from_now |
+----------------------+
| 1                    |
+----------------------+

/* First argument 'earlier' than second argument. */
select timestamp_cmp(now(), now() + interval 2 hours)
  as now_vs_2_hours_ago;
+--------------------+
| now_vs_2_hours_ago |
+--------------------+
| -1                 |
+--------------------+

/* Both arguments represent the same point in time. */

select timestamp_cmp(now(), now())
  as identical_timestamps;
+----------------------+
| identical_timestamps |
+----------------------+
| 0                    |
+----------------------+

select timestamp_cmp
(
  now() + interval 1 hour,
  now() + interval 60 minutes
) as equivalent_date_times;
+-----------------------+
| equivalent_date_times |
+-----------------------+
| 0                     |
+-----------------------+

/* Either argument NULL. */

select timestamp_cmp(now(), null)
  as now_vs_null;
+-------------+
| now_vs_null |
+-------------+
| NULL        |
+-------------+
TO_DATE(TIMESTAMP timestamp)
Purpose: Returns a string representation of the date field from a timestamp value.

Return type: STRING

Examples:

select now() as right_now,
  concat('The date today is ',to_date(now()),'.') as date_announcement;
+-------------------------------+-------------------------------+
| right_now                     | date_announcement             |
+-------------------------------+-------------------------------+
| 2016-06-01 16:30:36.890325000 | The date today is 2016-06-01. |
+-------------------------------+-------------------------------+
TO_TIMESTAMP(BIGINT unixtime), TO_TIMESTAMP(STRING date, STRING pattern)
Purpose: Converts an integer or string representing a date/time value into the corresponding TIMESTAMP value.

Return type: TIMESTAMP

Added in: CDH 5.5.0 / Impala 2.3.0

Usage notes:

An integer argument represents the number of seconds past the epoch (midnight on January 1, 1970). It is the converse of the unix_timestamp() function, which produces a BIGINT representing the number of seconds past the epoch.

A string argument, plus another string argument representing the pattern, turns an arbitrary string representation of a date and time into a true TIMESTAMP value. The ability to parse many kinds of date and time formats allows you to deal with temporal data from diverse sources, and if desired to convert to efficient TIMESTAMP values during your ETL process. Using TIMESTAMP directly in queries and expressions lets you perform date and time calculations without the overhead of extra function calls and conversions each time you reference the applicable columns.

Examples:

The following examples demonstrate how to convert an arbitrary string representation to TIMESTAMP based on a pattern string:

select to_timestamp('Sep 25, 1984', 'MMM dd, yyyy');
+----------------------------------------------+
| to_timestamp('sep 25, 1984', 'mmm dd, yyyy') |
+----------------------------------------------+
| 1984-09-25 00:00:00                          |
+----------------------------------------------+

select to_timestamp('1984/09/25', 'yyyy/MM/dd');
+------------------------------------------+
| to_timestamp('1984/09/25', 'yyyy/mm/dd') |
+------------------------------------------+
| 1984-09-25 00:00:00                      |
+------------------------------------------+

The following examples show how to convert a BIGINT representing seconds past epoch into a TIMESTAMP value:

-- One day past the epoch.
select to_timestamp(24 * 60 * 60);
+----------------------------+
| to_timestamp(24 * 60 * 60) |
+----------------------------+
| 1970-01-02 00:00:00        |
+----------------------------+

-- 60 seconds in the past.
select now() as 'current date/time',
  unix_timestamp(now()) 'now in seconds',
  to_timestamp(unix_timestamp(now()) - 60) as '60 seconds ago';
+-------------------------------+----------------+---------------------+
| current date/time             | now in seconds | 60 seconds ago      |
+-------------------------------+----------------+---------------------+
| 2017-10-01 22:03:46.885624000 | 1506895426     | 2017-10-01 22:02:46 |
+-------------------------------+----------------+---------------------+
TO_UTC_TIMESTAMP(TIMESTAMP, STRING timezone)
Purpose: Converts a specified timestamp value in a specified time zone into the corresponding value for the UTC time zone.

Return type: TIMESTAMP

Usage notes:

Often used in combination with the now() function, to translate local date and time values to the UTC time zone for consistent representation on disk. The opposite of the from_utc_timestamp() function.

See discussion of time zones in TIMESTAMP Data Type for information about using this function for conversions between the local time zone and UTC.

Examples:

The simplest use of this function is to turn a local date/time value to one with the standardized UTC time zone. Because the time zone specifier is not saved as part of the Impala TIMESTAMP value, all applications that refer to such data must agree in advance which time zone the values represent. If different parts of the ETL cycle, or different instances of the application, occur in different time zones, the ideal reference point is to convert all TIMESTAMP values to UTC for storage.

select now() as 'Current time in California USA',
  to_utc_timestamp(now(), 'PDT') as 'Current time in Greenwich UK';
+--------------------------------+-------------------------------+
| current time in california usa | current time in greenwich uk  |
+--------------------------------+-------------------------------+
| 2016-06-01 15:52:08.980072000  | 2016-06-01 22:52:08.980072000 |
+--------------------------------+-------------------------------+

Once a value is converted to the UTC time zone by to_utc_timestamp(), it can be converted back to the local time zone with from_utc_timestamp(). You can combine these functions using different time zone identifiers to convert a TIMESTAMP between any two time zones. This example starts with a TIMESTAMP value representing Pacific Daylight Time, converts it to UTC, and converts it to the equivalent value in Eastern Daylight Time.

select now() as 'Current time in California USA',
  from_utc_timestamp
  (
    to_utc_timestamp(now(), 'PDT'),
    'EDT'
  ) as 'Current time in New York, USA';
+--------------------------------+-------------------------------+
| current time in california usa | current time in new york, usa |
+--------------------------------+-------------------------------+
| 2016-06-01 18:14:12.743658000  | 2016-06-01 21:14:12.743658000 |
+--------------------------------+-------------------------------+
TRUNC(TIMESTAMP timestamp, STRING unit)
Purpose: Strips off fields from a TIMESTAMP value.
Unit argument: The unit argument value for truncating TIMESTAMP values is case-sensitive. This argument string can be one of:
  • SYYYY, YYYY, YEAR, SYEAR, YYY, YY, Y: Year.
  • Q: Quarter.
  • MONTH, MON, MM, RM: Month.
  • WW, W: Same day of the week as the first day of the month.
  • DDD, DD, J: Day.
  • DAY, DY, D: Starting day of the week. (Not necessarily the current day.)
  • HH, HH12, HH24: Hour. A TIMESTAMP value truncated to the hour is always represented in 24-hour notation, even for the HH12 argument string.
  • MI: Minute.

Added in: The ability to truncate numeric values is new starting in CDH 5.13 / Impala 2.10.

Usage notes:

The TIMESTAMP form is typically used in GROUP BY queries to aggregate results from the same hour, day, week, month, quarter, and so on. You can also use this function in an INSERT ... SELECT into a partitioned table to divide TIMESTAMP values into the correct partition.

Because the return value is a TIMESTAMP, if you cast the result of TRUNC() to STRING, you will often see zeroed-out portions such as 00:00:00 in the time field. If you only need the individual units such as hour, day, month, or year, use the EXTRACT() function instead. If you need the individual units from a truncated TIMESTAMP value, run the TRUNCATE() function on the original value, then run EXTRACT() on the result.

The trunc() function also has a signature that applies to DOUBLE or DECIMALvalues. truncate(), trunc(), and dtrunc() are all aliased to the same function. See truncate() under Impala Mathematical Functions for details.

Return type: TIMESTAMP

Examples:

The following example shows how the argument 'Q' returns a TIMESTAMP representing the beginning of the appropriate calendar quarter. This return value is the same for input values that could be separated by weeks or months. If you stored the trunc() result in a partition key column, the table would have four partitions per year.

select now() as right_now, trunc(now(), 'Q') as current_quarter;
+-------------------------------+---------------------+
| right_now                     | current_quarter     |
+-------------------------------+---------------------+
| 2016-06-01 18:32:02.097202000 | 2016-04-01 00:00:00 |
+-------------------------------+---------------------+

select now() + interval 2 weeks as 2_weeks_from_now,
  trunc(now() + interval 2 weeks, 'Q') as still_current_quarter;
+-------------------------------+-----------------------+
| 2_weeks_from_now              | still_current_quarter |
+-------------------------------+-----------------------+
| 2016-06-15 18:36:19.584257000 | 2016-04-01 00:00:00   |
+-------------------------------+-----------------------+
UNIX_TIMESTAMP(), UNIX_TIMESTAMP(STRING datetime), UNIX_TIMESTAMP(STRING datetime, STRING format), UNIX_TIMESTAMP(TIMESTAMP datetime)
Purpose: Returns a Unix time, which is a number of seconds elapsed since '1970-01-01 00:00:00' UTC. If called with no argument, the current date and time is converted to its Unix time. If called with arguments, the first argument represented as the TIMESTAMP or STRING is converted to its Unix time.

Return type: BIGINT

Usage notes:

See FROM_UNIXTIME() for details about the patterns you can use in the format string to represent the position of year, month, day, and so on in the date string. In Impala 1.3 and higher, you have more flexibility to switch the positions of elements and use different separator characters.

In CDH 5.4.3 and higher, you can include a trailing uppercase Z qualifier to indicate "Zulu" time, a synonym for UTC.

In CDH 5.5 / Impala 2.3 and higher, you can include a timezone offset specified as minutes and hours, provided you also specify the details in the format string argument. The offset is specified in the format string as a plus or minus sign followed by hh:mm, hhmm, or hh. The hh must be lowercase, to distinguish it from the HH represent hours in the actual time value. Currently, only numeric timezone offsets are allowed, not symbolic names.

In Impala 2.2.0 and higher, built-in functions that accept or return integers representing TIMESTAMP values use the BIGINT type for parameters and return values, rather than INT. This change lets the date and time functions avoid an overflow error that would otherwise occur on January 19th, 2038 (known as the "Year 2038 problem" or "Y2K38 problem"). This change affects the FROM_UNIXTIME() and UNIX_TIMESTAMP() functions. You might need to change application code that interacts with these functions, change the types of columns that store the return values, or add CAST() calls to SQL statements that call these functions.

UNIX_TIMESTAMP() and FROM_UNIXTIME() are often used in combination to convert a TIMESTAMP value into a particular string format. For example:
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(NOW() + interval 3 days),
  'yyyy/MM/dd HH:mm') AS yyyy_mm_dd_hh_mm;
+------------------+
| yyyy_mm_dd_hh_mm |
+------------------+
| 2016/06/03 11:38 |
+------------------+

The way this function deals with time zones when converting to or from TIMESTAMP values is affected by the ‑‑use_local_tz_for_unix_timestamp_conversions startup flag for the impalad daemon. See TIMESTAMP Data Type for details about how Impala handles time zone considerations for the TIMESTAMP data type.

Examples:

The following examples show different ways of turning the same date and time into an integer value. A format string that Impala recognizes by default is interpreted as a UTC date and time. The trailing Z is a confirmation that the timezone is UTC. If the date and time string is formatted differently, a second argument specifies the position and units for each of the date and time values.

The final two examples show how to specify a timezone offset of Pacific Daylight Saving Time, which is 7 hours earlier than UTC. You can use the numeric offset -07:00 and the equivalent suffix of -hh:mm in the format string, or specify the mnemonic name for the time zone in a call to to_utc_timestamp(). This particular date and time expressed in PDT translates to a different number than the same date and time expressed in UTC.

-- 3 ways of expressing the same date/time in UTC and converting to an integer.

select unix_timestamp('2015-05-15 12:00:00');
+---------------------------------------+
| unix_timestamp('2015-05-15 12:00:00') |
+---------------------------------------+
| 1431691200                            |
+---------------------------------------+

select unix_timestamp('2015-05-15 12:00:00Z');
+----------------------------------------+
| unix_timestamp('2015-05-15 12:00:00z') |
+----------------------------------------+
| 1431691200                             |
+----------------------------------------+

select unix_timestamp
(
  'May 15, 2015 12:00:00',
  'MMM dd, yyyy HH:mm:ss'
) as may_15_month_day_year;
+-----------------------+
| may_15_month_day_year |
+-----------------------+
| 1431691200            |
+-----------------------+

-- 2 ways of expressing the same date and time but in a different timezone.
-- The resulting integer is different from the previous examples.

select unix_timestamp
(
  '2015-05-15 12:00:00-07:00',
  'yyyy-MM-dd HH:mm:ss-hh:mm'
) as may_15_year_month_day;
+-----------------------+
| may_15_year_month_day |
+-----------------------+
| 1431716400            |
+-----------------------+

select unix_timestamp
  (to_utc_timestamp(
    '2015-05-15 12:00:00',
    'PDT')
  ) as may_15_pdt;
+------------+
| may_15_pdt |
+------------+
| 1431716400 |
+------------+
UTC_TIMESTAMP()
Purpose: Returns a TIMESTAMP corresponding to the current date and time in the UTC time zone.

Return type: TIMESTAMP

Added in: CDH 5.13

Usage notes:

Similar to the now() or current_timestamp() functions, but does not use the local time zone as those functions do. Use utc_timestamp() to record TIMESTAMP values that are interoperable with servers around the world, in arbitrary time zones, without the need for additional conversion functions to standardize the time zone of each value representing a date/time.

For working with date/time values represented as integer values, you can convert back and forth between TIMESTAMP and BIGINT with the unix_micros_to_utc_timestamp() and utc_to_unix_micros() functions. The integer values represent the number of microseconds since the Unix epoch (midnight on January 1, 1970).

Examples:

The following example shows how NOW() and CURRENT_TIMESTAMP() represent the current date/time in the local time zone (in this case, UTC-7), while UTC_TIMESTAMP() represents the same date/time in the standardized UTC time zone:

select now(), utc_timestamp();
+-------------------------------+-------------------------------+
| now()                         | utc_timestamp()               |
+-------------------------------+-------------------------------+
| 2017-10-01 23:33:58.919688000 | 2017-10-02 06:33:58.919688000 |
+-------------------------------+-------------------------------+

select current_timestamp(), utc_timestamp();
+-------------------------------+-------------------------------+
| current_timestamp()           | utc_timestamp()               |
+-------------------------------+-------------------------------+
| 2017-10-01 23:34:07.400642000 | 2017-10-02 06:34:07.400642000 |
+-------------------------------+-------------------------------+
WEEK(TIMESTAMP date), WEEKOFYEAR(TIMESTAMP date)
Purpose: Returns the corresponding week (1-53) from the date portion of a TIMESTAMP.

Return type: INT

Examples:

select now() as right_now, weekofyear(now()) as this_week;
+-------------------------------+-----------+
| right_now                     | this_week |
+-------------------------------+-----------+
| 2016-06-01 22:40:06.763771000 | 22        |
+-------------------------------+-----------+

select now() + interval 2 weeks as in_2_weeks,
  weekofyear(now() + interval 2 weeks) as week_after_next;
+-------------------------------+-----------------+
| in_2_weeks                    | week_after_next |
+-------------------------------+-----------------+
| 2016-06-15 22:41:22.098823000 | 24              |
+-------------------------------+-----------------+
WEEKS_ADD(TIMESTAMP date, INT weeks), WEEKS_ADD(TIMESTAMP date, BIGINT weeks)
Purpose: Returns the specified date and time plus some number of weeks.

Return type: TIMESTAMP

Examples:

select now() as right_now, weeks_add(now(), 2) as week_after_next;
+-------------------------------+-------------------------------+
| right_now                     | week_after_next               |
+-------------------------------+-------------------------------+
| 2016-06-01 22:43:20.973834000 | 2016-06-15 22:43:20.973834000 |
+-------------------------------+-------------------------------+
WEEKS_SUB(TIMESTAMP date, INT weeks), WEEKS_SUB(TIMESTAMP date, BIGINT weeks)
Purpose: Returns the specified date and time minus some number of weeks.

Return type: TIMESTAMP

Examples:

select now() as right_now, weeks_sub(now(), 2) as week_before_last;
+-------------------------------+-------------------------------+
| right_now                     | week_before_last              |
+-------------------------------+-------------------------------+
| 2016-06-01 22:44:21.291913000 | 2016-05-18 22:44:21.291913000 |
+-------------------------------+-------------------------------+
YEAR(TIMESTAMP date)
Purpose: Returns the year field from the date portion of a TIMESTAMP.

Return type: INT

Examples:

select now() as right_now, year(now()) as this_year;
+-------------------------------+-----------+
| right_now                     | this_year |
+-------------------------------+-----------+
| 2016-06-01 22:46:23.647925000 | 2016      |
+-------------------------------+-----------+
YEARS_ADD(TIMESTAMP date, INT years), YEARS_ADD(TIMESTAMP date, BIGINT years)
Purpose: Returns the specified date and time plus some number of years.

Return type: TIMESTAMP

Examples:

select now() as right_now, years_add(now(), 1) as next_year;
+-------------------------------+-------------------------------+
| right_now                     | next_year                     |
+-------------------------------+-------------------------------+
| 2016-06-01 22:47:45.556851000 | 2017-06-01 22:47:45.556851000 |
+-------------------------------+-------------------------------+

The following example shows how if the equivalent date does not exist in the year of the result due to a leap year, the date is changed to the last day of the appropriate month.

-- Spoiler alert: there is no Feb. 29, 2017
select cast('2016-02-29' as timestamp) as feb_29_2016,
  years_add('2016-02-29', 1) as feb_29_2017;
+---------------------+---------------------+
| feb_29_2016         | feb_29_2017         |
+---------------------+---------------------+
| 2016-02-29 00:00:00 | 2017-02-28 00:00:00 |
+---------------------+---------------------+
YEARS_SUB(TIMESTAMP date, INT years), YEARS_SUB(TIMESTAMP date, BIGINT years)
Purpose: Returns the specified date and time minus some number of years.

Return type: TIMESTAMP

Examples:

select now() as right_now, years_sub(now(), 1) as last_year;
+-------------------------------+-------------------------------+
| right_now                     | last_year                     |
+-------------------------------+-------------------------------+
| 2016-06-01 22:48:11.851780000 | 2015-06-01 22:48:11.851780000 |
+-------------------------------+-------------------------------+

The following example shows how if the equivalent date does not exist in the year of the result due to a leap year, the date is changed to the last day of the appropriate month.

-- Spoiler alert: there is no Feb. 29, 2015
select cast('2016-02-29' as timestamp) as feb_29_2016,
  years_sub('2016-02-29', 1) as feb_29_2015;
+---------------------+---------------------+
| feb_29_2016         | feb_29_2015         |
+---------------------+---------------------+
| 2016-02-29 00:00:00 | 2015-02-28 00:00:00 |
+---------------------+---------------------+