Temporal Functions

Table 1 lists the temporal functions supported by Flink OpenSource SQL.

Function Description

Table 1 Temporal functions

Function

Return Type

Description

DATE string

DATE

Parse the date string (yyyy-MM-dd) to a SQL date.

TIME string

TIME

Parse the time string (HH:mm:ss[.fff]) to a SQL time.

TIMESTAMP string

TIMESTAMP

Convert the time string into a timestamp. The time string format is yyyy-MM-dd HH:mm:ss[.fff].

INTERVAL string range

INTERVAL

Parse an interval string in the following two forms:

  • yyyy-MM for SQL intervals of months. An interval range might be YEAR or YEAR TO MONTH.
  • dd hh:mm:ss.fff for SQL intervals of milliseconds. An interval range might be DAY, MINUTE, DAY TO HOUR, or DAY TO SECOND.

Example:

INTERVAL '10 00:00:00.004' DAY TO second indicates that the interval is 10 days and 4 milliseconds.

INTERVAL '10' DAY: indicates that the interval is 10 days.

INTERVAL '2-10' YEAR TO MONTH indicates that the interval is two years and ten months.

CURRENT_DATE

DATE

Return the SQL date of UTC time zone.

CURRENT_TIME

TIME

Return the SQL time of UTC time zone.

CURRENT_TIMESTAMP

TIMESTAMP

Return the SQL timestamp of UTC time zone.

LOCALTIME

TIME

Return the SQL time of the local time zone.

LOCALTIMESTAMP

TIMESTAMP

Return the SQL timestamp of the local time zone.

EXTRACT(timeintervalunit FROM temporal)

BIGINT

Extract part of the time point or interval. Return the part in the int type.

For example, extract the date 2006-06-05 and return 5.

EXTRACT(DAY FROM DATE '2006-06-05') returns 5.

YEAR(date)

BIGINT

Return the year from a SQL date.

For example, YEAR(DATE'1994-09-27') returns 1994.

QUARTER(date)

BIGINT

Return the quarter of a year from a SQL date.

MONTH(date)

BIGINT

Return the month of a year from a SQL date.

For example, MONTH(DATE '1994-09-27') returns 9.

WEEK(date)

BIGINT

Return the week of a year from a SQL date.

For example, WEEK(DATE'1994-09-27') returns 39.

DAYOFYEAR(date)

BIGINT

Return the day of a year from a SQL date.

For example, DAYOFYEAR(DATE '1994-09-27') is 270.

DAYOFMONTH(date)

BIGINT

Return the day of a month from a SQL date.

For example, DAYOFMONTH(DATE'1994-09-27') returns 27.

DAYOFWEEK(date)

BIGINT

Return the day of a week from a SQL date.

Sunday is set to 1.

For example, DAYOFWEEK(DATE'1994-09-27') returns 3.

HOUR(timestamp)

BIGINT

Return the hour of a day (an integer between 0 and 23) from a SQL timestamp.

For example, HOUR(TIMESTAMP '1994-09-27 13:14:15') returns 13.

MINUTE(timestamp)

BIGINT

Return the minute of an hour (an integer between 0 and 59) from a SQL timestamp.

For example, MINUTE(TIMESTAMP '1994-09-27 13:14:15') returns 14.

SECOND(timestamp)

BIGINT

Returns the second of a minute (an integer between 0 and 59) from a SQL timestamp.

For example, SECOND(TIMESTAMP '1994-09-27 13:14:15') returns 15.

FLOOR(timepoint TO timeintervalunit)

TIME

Round a time point down to the given unit.

For example, 12:44:00 is returned from FLOOR(TIME '12:44:31' TO MINUTE).

CEIL(timepoint TO timeintervalunit)

TIME

Round a time point up to the given unit.

For example, CEIL(TIME '12:44:31' TO MINUTE) returns 12:45:00.

(timepoint1, temporal1) OVERLAPS (timepoint2, temporal2)

BOOLEAN

Return TRUE if two time intervals overlap.

Example:

(TIME '2:55:00', INTERVAL '1' HOUR) OVERLAPS (TIME '3:30:00', INTERVAL '2' HOUR) returns TRUE.

(TIME '9:00:00', TIME '10:00:00') OVERLAPS (TIME '10:15:00', INTERVAL '3' HOUR) returns FALSE.

DATE_FORMAT(timestamp, string)

STRING

Convert a timestamp to a value of string in the format specified by the date format string.

TIMESTAMPADD(timeintervalunit, interval, timepoint)

TIMESTAMP/DATE/TIME

Return the date and time added to timepoint based on the result of interval and timeintervalunit.

For example, TIMESTAMPADD(WEEK, 1, DATE '2003-01-02') returns 2003-01-09.

TIMESTAMPDIFF(timepointunit, timepoint1, timepoint2)

INT

Return the (signed) number of timepointunit between timepoint1 and timepoint2.

The unit for the interval is given by the first argument, which should be one of the following values: SECOND, MINUTE, HOUR, DAY, MONTH, and YEAR.

For example, TIMESTAMPDIFF(DAY, TIMESTAMP '2003-01-02 10:00:00', TIMESTAMP '2003-01-03 10:00:00') returns 1.

CONVERT_TZ(string1, string2, string3)

TIMESTAMP

Convert a datetime string1 from time zone string2 to time zone string3.

For example, CONVERT_TZ('1970-01-01 00:00:00', 'UTC', 'America/Los_Angeles') returns '1969-12-31 16:00:00'.

FROM_UNIXTIME(numeric[, string])

STRING

Return a representation of the numeric argument as a value in string format.

The default string format is YYYY-MM-DD hh:mm:ss.

For example, FROM_UNIXTIME(44) returns 1970-01-01 09:00:44.

UNIX_TIMESTAMP()

BIGINT

Get current Unix timestamp in seconds.

UNIX_TIMESTAMP(string1[, string2])

BIGINT

Convert date time string string1 in format string2 to Unix timestamp (in seconds), using the specified timezone in table config.

The default format of string2 is yyyy-MM-dd HH:mm:ss.

TO_DATE(string1[, string2])

DATE

Convert a date string string1 with format string2 to a date.

The default format of string2 is yyyy-MM-dd.

TO_TIMESTAMP(string1[, string2])

TIMESTAMP

Convert date time string string1 with format string2 to a timestamp.

The default format of string2 is yyyy-MM-dd HH:mm:ss.

DATE

TIME

TIMESTAMP

INTERVAL

CURRENT_DATE

CURRENT_TIME

CURRENT_TIMESTAMP

LOCALTIME

LOCALTIMESTAMP

EXTRACT

YEAR

QUARTER

MONTH

WEEK

DAYOFYEAR

DAYOFMONTH

DAYOFWEEK

HOUR

MINUTE

SECOND

FLOOR

CEIL

OVERLAPS

DATE_FORMAT

TIMESTAMPADD

TIMESTAMPDIFF

CONVERT_TZ

FROM_UNIXTIME

UNIX_TIMESTAMP

UNIX_TIMESTAMP(string1[, string2])

TO_DATE

TO_TIMESTAMP