Date Time Functions

These apply to MonetDB SQL temporal types: date, time, time with time zone, timestamp and timestamp with time zone.

Some functions apply to MonetDB SQL types: second interval, day interval and month interval.

Date/Time literal formats

Cast prefixDescriptionExample
dateconvert YYYY-MM-DD formatted string value to a date typeselect date '2020-09-28'
{dconvert YYYY-MM-DD formatted string value to a date type using ODBC escape sequence notationselect { d '2020-09-28' }
timeconvert HH:MI:SS formatted string value to a time typeselect time '18:40:05'
{tconvert HH:MI:SS formatted string value to a time type using ODBC escape sequence notationselect { t '18:40:05' }
time(6)convert HH:MI:SS.ssssss formatted string value to a time(6) typeselect time(6) '18:40:05.123456'
time(3) with time zoneconvert HH:MI:SS.sss+MI:SS formatted string value to a time(3) with time zone typeselect time(3) with time zone '18:40:05.123+02:00'
timestampconvert YYYY-MM-DD HH:MI:SS formatted string value to a timestamp typeselect timestamp '2020-09-28 18:40:05'
{tsconvert YYYY-MM-DD HH:MI:SS formatted string value to a timestamp type using ODBC escape sequence notationselect {ts '2020-09-28 18:40:05'}
timestamp(6)convert YYYY-MM-DD HH:MI:SS.ssssss formatted string value to a timestamp(6) typeselect timestamp(6) '2020-09-28 18:40:05.123456'
timestamp(3) with time zoneconvert YYYY-MM-DD HH:MI:SS.sss+MI:SS formatted string value to a timestamp(3) with time zone typeselect timestamp(3) with time zone '2020-09-28 18:40:05.123+02:00'
intervalconvert number value string to second interval typeselect interval '67205' second
intervalconvert number value string to day interval typeselect interval '30' day
intervalconvert number value string to month interval typeselect interval '6' month
intervalconvert HH:MI:SS formatted string value to second interval typeselect interval '18:40:05' hour to second
intervalconvert DD HH:MI:SS formatted string value to second interval typeselect interval '2 18:40:05.123' day to second(3)
intervalconvert YYYY-MM formatted string value to month interval typeselect interval '2-5' year to month
{intervalconvert number value string to second interval type using ODBC escape sequence notationselect {interval '67205' second}
{intervalconvert number value string to day interval type using ODBC escape sequence notationselect {interval '30' day}
{intervalconvert number value string to month interval type using ODBC escape sequence notationselect {interval '6' month}
{intervalconvert HH:MI:SS formatted string value to second interval type using ODBC escape sequence notationselect {interval '18:40:05' hour to second}
{intervalconvert DD HH:MI:SS formatted string value to second interval type using ODBC escape sequence notationselect {interval '2 18:40:05.123' day to second(3)}
{intervalconvert YYYY-MM formatted string value to month interval type using ODBC escape sequence notationselect {interval '2-5' year to month}

Date/Time operators

OperatorDescriptionExampleResult
+add seconds to a date or time or timestampdate '2020-09-28' + (interval '1' second * (7 * 24 * 60 * 60))date '2020-10-05'
-subtract seconds from a date or timedate '2020-09-28' - (interval '1' second * (7 * 24 * 60 * 60))date '2020-09-21'
-subtract months from a date or timestampdate '2020-09-28' - interval '7' monthdate '2020-02-28'
-subtract two datesdate '2020-09-28' - date '2020-02-28'interval '213' day
-subtract two timestime '14:35:45' - time '02:12:24'interval '12:23:21' hour to second

Date/Time functions

FunctionReturn typeDescriptionExampleResult
century(date)intextract century from a date or timestamp or timestamptzcentury(date '2020-03-22')21
curdate()dateget current datecurdate()date '2020-03-22'
current_date()dateget current datecurrent_date()date '2020-03-22'
current_time()timetzget current time with time zonecurrent_time()timetz '13:16:57.734639+01:00'
current_timestamp()timestamptzget current timestamp (date and time and fractions) with time zonecurrent_timestamp()timestamptz '2020-03-22 13:16:57.734639+01:00'
curtime()timetzget current time with time zonecurtime()timetz '13:16:57.734639+01:00'
sys.date_to_str(date, format_str)clobconvert date value to a string using format_str. See format specifierssys.date_to_str(date '2020-03-22', '%D %d.%m.%Y');03/22/20 22.03.2020
sys.date_trunc(field_str, timestamp)timestampTruncate a timestamp to granularity specified bij field_str. Allowed values for field_str are 'millennium', 'century', 'decade', 'year', 'quarter', 'month', 'week', 'day', 'hour', 'minute', 'second', 'milliseconds', 'microseconds'.sys.date_trunc('month', timestamp '2020-03-22 13:16:57.734639');timestamp '2020-03-01 00:00:00.000000'
"day"(dt_or_ts)intextract month day nr (1..31) of a date or timestamp or timestamptz. same as: dayofmonth(date)"day"(date '2020-03-22')22
"day"(sec_interval)bigintcompute nr of days and truncate it to an integer"day"(interval '3.23' second * (24 * 60 * 60))3
{fn DAYNAME(dt_or_ts)}varcharextract day name in current locale of a date or timestamp or timestamptz. This function requires the ODBC escape sequence notation.select {fn DAYNAME({d '2020-03-22'})}Sunday
dayofmonth(dt_or_ts)intextract month day nr (1..31) of a date or timestamp or timestamptz. same as: "day"(date)dayofmonth(date '2020-03-22')22
dayofweek(dt_or_ts)intextract week day nr (1..7) of a date or timestamp or timestamptzdayofweek(date '2020-03-22')7
dayofyear(dt_or_ts)intextract year day nr (1..366) of a date or timestamp or timestamptzdayofyear(date '2020-03-22')82
decade(dt_or_ts)intextract decade nr of a date or timestamp or timestamptzdecade(date '2027-03-22')202
sys.epoch(int seconds)timestamptzadd number of seconds to timestamp '1970-01-01 00:00:00.0'sys.epoch(1234567890)timestamptz '2009-02-14 01:31:30.000000+02:00'
sys.epoch(decimal(18,3) seconds)timestamptzadd number of seconds to timestamp '1970-01-01 00:00:00.0'sys.epoch(1234567890.456)timestamptz '2009-02-14 01:31:30.456000+02:00'
sys.epoch(timestamp)intconvert timestamp (or timestamptz) to nr of seconds since '1970-01-01 00:00:00.0'sys.epoch(timestamp '2009-02-13 23:31:30.0')1234567890
epoch_ms(dt_or_tm_or_ts_or_interval)decimal(18,3)convert date or time or timestamp or interval to number of milliseconds since '1970-01-01 00:00:00.0'epoch_ms(timestamp '2009-02-13 23:31:30.0')1234567890
extract(field from dt_value)intextract a specific field from a date or time or timestamp. Supported keywords for field are: CENTURY, DECADE, YEAR, QUARTER, MONTH, WEEK, DAY, DOW, DOY, HOUR, MINUTE and SECOND.extract(doy from date '2027-03-22')81
greatest(x, y)same as inputgreatest (highest) value of x and y (same as sql_max(x,y))greatest(date '2020-03-22', date '2020-03-25')date '2020-03-25'
"hour"(tm_or_ts)intextract hour nr (0..23) of a time or timetz or timestamp or timestamptz or sec_interval"hour"(timetz '15:35:02.002345+01:00')16
least(x, y)same as inputleast (lowest) value of x and y (same as sql_min(x,y))least(time '15:15:15', time '16:16:16')time '15:15:15'
local_timezone()sec_intervalget local time zone as represented in secondslocal_timezone()7200
localtime()timeget current local time without time zonelocaltime()time '13:16:57.734639'
localtimestamp()timestampget current local timestamp (date and time and fractions) without time zonelocaltimestamp()timestamp '2020-03-22 13:16:57.734639'
"minute"(tm_or_ts)intextract minutes (0..59) of a time or timetz or timestamp or timestamptz or sec_interval"minute"(timetz '15:35:02.002345+01:00')35
"month"(dt_or_ts)intextract month nr (1..12) of a date or timestamp or timestamptz or month_interval"month"(date '2020-07-22')7
{fn MONTHNAME(dt_or_ts)}varcharextract month name in current locale of a date or timestamp or timestamptz. This function requires the ODBC escape sequence notation.select {fn MONTHNAME({d '2020-03-22'})}March
now()timestamptzget current timestamp (date and time and fractions) with time zonenow()timestamptz '2020-03-22 13:16:57.734639+01:00'
quarter(dt_or_ts)intextract quarter nr (1..4) of a date or timestamp or timestamptzquarter(date '2020-07-22')3
"second"(sec_interval)intextract seconds (0 .. 59) of a sec_interval"second"(interval '24' second)24
"second"(tm_or_ts)decimal(9,6)extract seconds (0.000 .. 59.999999) of a time or timetz or timestamp or timestamptz"second"(timetz '15:35:02.002345+01:00')2.002345
sql_add(dt_or_ts, month_interval)same as arg 1add an integer month interval value to a date or timestamp or timestamptzsql_add(date '2021-12-31', interval '2' month)date '2022-02-28'
sql_add(dt_or_tm, sec_interval)same as arg 1add a decimal seconds interval value to a date or time or timetz or timestamp or timestamptzsql_add(time '15:35:50', interval '50' second)time '15:36:40'
sql_max(x, y)same as inputgreatest (highest) value of x and y (same as greatest(x,y))sql_max(date '2020-03-22', date '2020-03-25')date '2020-03-25'
sql_min(x, y)same as inputleast (lowest) value of x and y(same as least(x,y))sql_min(time '15:15:15', time '16:16:16')time '15:15:15'
sql_sub(date, date)intsubtract a date from a date returning nr of seconds between the two datessql_sub(date '2021-12-31', date '2021-12-11')interval '20' day
sql_sub(dt_or_ts, month_interval)same as arg 1subtract an integer month interval value from a date or timestamp or timestamptzselect sql_sub(date '2021-12-31', interval '-2' month)date '2022-02-28'
sql_sub(dt_or_tm, sec_interval)same as arg 1subtract a decimal seconds interval value from a date or time or timetz or timestamp or timestamptzselect sql_sub(time '15:35:50', interval '3:25' minute to second)time '15:32:25'
sql_sub(time, time)sec_intervalsubtract a time from a time returning nr of seconds interval between the two timessql_sub(time '9:00:00', time '17:00:00')interval '-28800' second
sql_sub(timestamp, timestamp)sec_intervalsubtract a timestamp from a timestamp returning nr of seconds interval between the two timestampssql_sub(timestamp '2021-12-31 18:00:00', timestamp '2021-12-31 17:00:00')interval '3600' second
sql_sub(timestamptz, timestamptz)sec_intervalsubtract a timestamptz from a timestamptz returning nr of seconds interval between the two timestamps with time zonessql_sub(timestamptz '2021-12-31 18:00:00+02:00', timestamptz '2021-12-31 18:00:00-02:00')interval '-14400' second
sql_sub(timetz, timetz)sec_intervalsubtract a timetz from a timetz returning nr of seconds interval between the two timetzssql_sub(timetz '18:00:00+02:00', timetz '18:00:00-02:00')interval '-14400' second
sys.str_to_date(s, format_str)dateparse string s using format_str and construct a date value. See format specifierssys.str_to_date('4/30/2021', '%m/%d/%Y')date '2021-04-30'
sys.str_to_time(s, format_str)timetzparse string s using format_str and construct a time(0) with time zone value. See format specifierssys.str_to_time('09:30:49','%H:%M:%S')time(0) with time zone '11:30:49+02:00'
sys.str_to_timestamp(s, format_str)timestamptzparse string s using format_str and construct a timestamp with time zone value. See format specifierssys.str_to_timestamp('4/30/2021 09:30:49','%m/%d/%Y %H:%M:%S')timestamptz '2021-04-30 11:30:49.000000+02:00'
sys.time_to_str(timetz, format_str)clobconvert time with time zone value to a string using format_str. See format specifierssys.time_to_str(timetz '19:30:49', '%l hour %M:%S %p')' 7 hour 30:49 PM'
sys.timestamp_to_str(timestamptz, format_str)clobconvert timestamp with time zone value to a string using format_str. See format specifierssys.timestamp_to_str(timestamptz '2021-12-31 18:00:00', '%m/%d/%Y %l:%M:%S %p')'12/31/2021 6:00:00 PM'
timestampadd(dt_ts_tm, interval)timestamp or timestamptzadd interval (month or day or second) to a timestamp (with time zone) or date or timeselect timestampadd(timestamp '2021-12-31 18:00:00', interval '2' month)timestamp '2022-02-28 18:00:00'
timestampdiff(ts_tstz, ts_tstz)interval secondreturns difference of two timestamps or two timestamps with time zoneselect timestampdiff(timestamp '2021-12-31 18:40:40', timestamp '2021-12-30 16:30:20')interval '94220' second
timestampdiff_day(ts_dt_tz, ts_dt_tz)integerreturns difference of two timestamps or timestamps with time zone or datesselect timestampdiff_day(timestamp '2021-12-31 18:40:40', timestamp '2021-12-20 16:30:20')11
timestampdiff_hour(ts_dt_tz, ts_dt_tz)bigintreturns difference of two timestamps or timestamps with time zone or datesselect timestampdiff_hour(timestamp '2021-12-31 18:40:40', timestamp '2021-12-20 16:30:20')266
timestampdiff_min(ts_dt_tz, ts_dt_tz)bigintreturns difference of two timestamps or timestamps with time zone or datesselect timestampdiff_min(timestamp '2021-12-31 18:40:40', timestamp '2021-12-31 16:30:20')130
timestampdiff_month(ts_tm_tz, ts_tm_tz)integerreturns difference of two timestamps or two timestamps with time zoneselect timestampdiff_month(timestamp '2021-12-31 18:40:40', timestamp '2021-02-20 16:30:20')10
timestampdiff_quarter(ts_tm_tz, ts_tm_tz)integerreturns difference of two timestamps or two timestamps with time zoneselect timestampdiff_quarter(timestamp '2021-12-31 18:40:40', timestamp '2021-02-20 16:30:20')3
timestampdiff_sec(ts_dt_tz, ts_dt_tz)bigintreturns difference of two timestamps or timestamps with time zone or datesselect timestampdiff_sec(timestamp '2021-12-31 18:40:40', timestamp '2021-12-31 16:30:20')7820
timestampdiff_week(ts_tm_tz, ts_tm_tz)integerreturns difference of two timestamps or two timestamps with time zoneselect timestampdiff_week(timestamp '2021-12-31 18:40:40', timestamp '2021-02-20 16:30:20')44
timestampdiff_year(ts_tm_tz, ts_tm_tz)integerreturns difference of two timestamps or two timestamps with time zoneselect timestampdiff_year(timestamp '2021-12-31 18:40:40', timestamp '2024-02-20 16:30:20')-3
usweekofyear(date)intextract us week number (1 .. 53) of a date. The US system has weeks from Sunday through Saturday, and partial weeks at the beginning and the end of the year.usweekofyear(date '2020-03-22')13
week(dt_or_ts)intextract ISO 8601 week number (1 .. 53) of a date or timestamp or timestamptz. same as: weekofyear(date)week(date '2020-03-22')12
weekofyear(dt_or_ts)intextract ISO 8601 week number (1 .. 53) of a date or timestamp or timestamptz. same as: week(date)weekofyear(date '2020-03-22')12
"year"(dt_or_ts)intextract year number of a date or timestamp or timestamptz or month_interval"year"(date '2020-03-22')2020

Date/Time pseudo columns

Pseudo columnDescriptionExampleResult
CURRENT_DATEget current dateselect current_datedate '2020-02-02'
CURRENT_TIMEget current time (with time zone)select current_timetimetz '15:35:02.073522+01:00'
CURRENT_TIMESTAMPget current timestamp (with time zone)select current_timestamptimestamptz '2020-02-02 15:35:02.002345+01:00'
CURRENT_TIMEZONEget current time zone as seconds intervalselect current_timezone7200.000
NOWsame as: CURRENT_TIMESTAMPselect nowtimestamptz '2020-02-02 15:35:02.002345+01:00'
LOCALTIMEget current local time without time zoneselect localtimetime '15:35:02.073522'
LOCALTIMESTAMPget current local timestamp without time zoneselect localtimestamptimestamp '2020-02-02 15:35:02.073522'

Date/Time format specifiers

All possible format string specifiers are listed below (taken from Linux Programmer’s Manual)

codedescription
%aThe abbreviated name of the day of the week according to the current locale.
%AThe full name of the day of the week according to the current locale.
%bThe abbreviated month name according to the current locale.
%BThe full month name according to the current locale.
%cThe preferred date and time representation for the current locale.
%CThe century number (year/100) as a 2-digit integer. (SU)
%dThe day of the month as a decimal number (range 01 to 31).
%DEquivalent to %m/%d/%y. (Yecch—for Americans only. Americans should note that in other countries %d/%m/%y is rather common. This means that in international context this format is ambiguous and should not be used.) (SU)
%eLike %d, the day of the month as a decimal number, but a leading zero is replaced by a space. (SU)
%EModifier: use alternative format, see below. (SU)
%FEquivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
%GThe ISO 8601 week-based year with century as a decimal number. The 4-digit year corresponding to the ISO week number (see %V). This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead. (TZ)
%gLike %G, but without century, that is, with a 2-digit year (00-99). (TZ)
%hEquivalent to %b. (SU)
%HThe hour as a decimal number using a 24-hour clock (range 00 to 23).
%IThe hour as a decimal number using a 12-hour clock (range 01 to 12).
%jThe day of the year as a decimal number (range 001 to 366).
%kThe hour (24-hour clock) as a decimal number (range 0 to 23); single digits are preceded by a blank. (See also %H.) (TZ)
%lThe hour (12-hour clock) as a decimal number (range 1 to 12); single digits are preceded by a blank. (See also %I.) (TZ)
%mThe month as a decimal number (range 01 to 12).
%MThe minute as a decimal number (range 00 to 59).
%nA newline character. (SU)
%OModifier: use alternative format, see below. (SU)
%pEither "AM" or "PM" according to the given time value, or the corresponding strings for the current locale. Noon is treated as "PM" and midnight as "AM".
%PLike %p but in lowercase: "am" or "pm" or a corresponding string for the current locale. (GNU)
%rThe time in a.m. or p.m. notation. In the POSIX locale this is equivalent to %I:%M:%S %p. (SU)
%RThe time in 24-hour notation (%H:%M). (SU) For a version including the seconds, see %T below.
%sThe number of seconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC). (TZ)
%SThe second as a decimal number (range 00 to 60). (The range is up to 60 to allow for occasional leap seconds.)
%tA tab character. (SU)
%TThe time in 24-hour notation (%H:%M:%S). (SU)
%uThe day of the week as a decimal, range 1 to 7, Monday being 1. See also %w. (SU)
%UThe week number of the current year as a decimal number, range 00 to 53, starting with the first Sunday as the first day of week 01. See also %V and %W.
%VThe ISO 8601 week number of the current year as a decimal number, range 01 to 53, where week 1 is the first week that has at least 4 days in the new year. See also %U and %W. (SU)
%wThe day of the week as a decimal, range 0 to 6, Sunday being 0. See also %u.
%WThe week number of the current year as a decimal number, range 00 to 53, starting with the first Monday as the first day of week 01.
%xThe preferred date representation for the current locale without the time.
%XThe preferred time representation for the current locale without the date.
%yThe year as a decimal number without a century (range 00 to 99).
%YThe year as a decimal number including the century.
%zThe +hhmm or -hhmm numeric timezone (that is, the hour and minute offset from UTC). (SU)
%ZThe timezone name or abbreviation.
%+The date and time in date(1) format. (TZ) (Not supported in glibc2.)
%%A literal '%' character.