Datetime Functions and Operators

Manipulate the datetime values with the following datetime functions supported by the query service.

Date and timestamp formats supported by the datetime functions. Use a hyphen - as a separator between the date values (year, month, week, and day) and a colon : as a separator between time values (hours, minutes, and seconds). If subseconds are to be specified, use a period . as a separator.

  • yyyy-MM-dd HH:mm:ss.SSSZZZZ
  • yyyy-MM-dd HH:mm:ss.SSS

Units of time supported by the datetime functions.

  • millisecond
  • second
  • minute
  • hour
  • day
  • week
  • month
  • quarter
  • year
FunctionReturn TypeInput ArgumentsDescription
current_date()DateNoneReturns the current date as of the start of the query.
current_time()TimeNoneReturns the current time as of the start of the query.
current_timestamp(p)Timestamp with time zoneTakes no input arguments by default.
  • p: Precision value of subseconds (optional)
Returns the current timestamp with time zone as of the start of the query, with precision up to three digits of subsecond accuracy. You can optionally specify the precision of subseconds required.
localtime()TimeNoneReturns the current local time at the start of the query.
localtimestamp()Timestamp with time zoneNoneReturns the current local timestamp as of at the start of the query, up to three-digit subsecond precision.
current_timezone()VarcharNoneReturns the timezone of the server.
date_add(unit, interval, timestamp)Same as input timestamp.
  • unit: Unit of time
  • interval: Time
  • timestamp: Timestamp
Use this function to add an interval value of type unit to the input timestamp. You can use a negative interval value and perform subtraction on the input timestamp. For example:
  • SELECT date_add('day', 10, TIMESTAMP '2022-04-06 12:39:25') returns 2022-04-16 12:39:25
date_diff(unit, timestamp1, timestamp2)Bigint
  • unit: Unit of time
  • timestamp1: Timestamp
  • timestamp2: Timestamp
Use this function to find the difference between two input timestamps. It returns timestamp2 minus timestamp1 expressed in terms of the specified unit of time. For example:
  • SELECT date_diff('day', TIMESTAMP '2022-04-06 12:39:25', TIMESTAMP '2022-06-06 12:39:25') returns 62
date_format(timestamp, formating_string)Varchar
  • timestamp: Timestamp
  • formating_string: String
Returns a formatted timestamp as a string using the specified input format. Refer to Format Specifiers to learn more about constructing the formatting string.
date_parse(string, formatting_string)Timestamp
  • string: String
  • formatting_string: String
Parses the input string into a timestamp using the input format. Refer to Format Specifiers to learn more about constructing the formatting string. For example:
  • select date_parse('2016/7/22 6:05:04 PM','%Y/%m/%d %h:%i:%s %p') returns 2016-07-22T18:05:04.000+00:00
date_trunc(unit, timestamp)Timestamp
  • unit: Unit of time
  • timestamp: Timestamp
Returns the truncated input timestamp by the input units. For example:
  • SELECT date_trunc('day', TIMESTAMP '2001-08-22 03:04:05.321') returns 2001-08-22T00:00:00.000+00:00
from_iso8601_date(string)Date
  • string: An ISO 8601 formatted date string
Parses the date string into a date. The input string can be a calendar date, a week date using ISO week numbering, or year and day of year combined. For example:
  • SELECT from_iso8601_date('2014-W50') returns 2014-12-08
  • SELECT from_iso8601_date('2014-143') returns 2014-05-23
from_iso8601_timestamp(string)Timestamp with time zone
  • string: String
Parses ISO 8601 formatted date string into a timestamp. The input string can be a calendar date, a week date using ISO week numbering, or year and day of year combined. For example:
  • SELECT from_iso8601_timestamp('2020-05-11') returns 2020-05-11T00:00:00.000+00:0
  • SELECT from_iso8601_timestamp('2020-W45') returns 2020-11-02T00:00:00.000+00:00
  • SELECT from_iso8601_timestamp('2020-234') returns 2020-08-21T00:00:00.000+00:00
from_unixtime(unixtime_in_seconds)Timestamp with time zone
  • unixtime_in_seconds: Unix time in seconds
Converts the Unix time in seconds to date and timestamp. The unixtime_in_seconds is the number of seconds since 1970-01-01 00:00:00 UTC. For example:
  • SELECT from_unixtime(1395379200.25) returns 2014-03-21T05:20:00.250+00:00
from_unixtime_nanos(unixtime_in_ns)Timestamp with time zone
  • unixtime_in_ns: Unix time in nanoseconds
Returns the UNIX timestamp as a timestamp with time zone. The argument unixtime_in_ns is the number of nanoseconds since 1970-01-01 00:00:00.000000000 UTC.
human_readable_seconds(seconds)Varchar
  • seconds: Double
Converts the input in seconds into a human-readable string with weeks, days, hours, minutes, and seconds. For example:
  • SELECT human_readable_seconds(109537920075) returns 181,114 weeks, 2 days, 1 minute, 15 seconds
now()Timestamp with time zoneNoneReturns the current timestamp with time zone as of the start of the query, with precision up to three digits of subsecond accuracy.
parse_duration(string)Interval
  • string: String in the format value unit
Parses the input string with the format value unit into a time interval, where value is a fractional number and unit is the unit of time. For example:
  • SELECT parse_duration('42.8ms') returns "milliSeconds": 43
to_iso8601(arg)Varchar
  • arg: Date, timestamp, or timestamp with time zone
Formats the input as an ISO 8601 string. For example:
  • SELECT to_iso8601(TIMESTAMP '2020-05-11 11:15:05') returns 2020-05-11T11:15:05
with_timezone(timestamp, timezone)Timestamp with time zone
  • timestamp: Timestamp
  • timezone: String specifying a timezone
Returns a timestamp with time zone from timestamp with precision p and timezone. For example:
  • SELECT with_timezone( TIMESTAMP '2020-03-01 00:00:00', 'Indian/Mauritius') returns 2020-02-29T20:00:00.000+00:00
Format SpecifierUsage
%aAbbreviated weekday name (Sun to Sat)
%bAbbreviated month name (Jan to Dec)
%cMonth, numeric (1 to 12)
%DDay of the month with English suffix (0th, 1st, 2nd, 3rd, and so on)
%dDay of the month, numeric (01 to 31)
%eDay of the month, numeric (1 to 31)
%fFraction of second (six digits for printing: 000000 to 999000; one to nine digits for parsing: 0 to 999999999)
%HHour (00 to 23)
%hHour (01 to 12)
%IHour (01 to 12)
%iMinutes, numeric (00 to 59)
%jDay of year (001 to 366)
%kHour (0 to 23)
%lHour (1 to 12)
%MMonth name (January to December)
%mMonth, numeric (01 to 12)
%pAM or PM
%rTime of day, 12-hour (equivalent to %h:%i:%s %p)
%SSeconds (00 to 59)
%sSeconds (00 to 59)
%TTime of day, 24-hour (equivalent to %H:%i:%s)
%UWeek (00 to 53), where Sunday is the first day of the week
%uWeek (00 to 53), where Monday is the first day of the week
%VWeek (01 to 53), where Sunday is the first day of the week; used with %X
%vWeek (01 to 53), where Monday is the first day of the week; used with %x
%WWeekday name (Sunday to Saturday)
%wDay of the week (0 to 6), where Sunday is the first day of the week
%XYear for the week where Sunday is the first day of the week, numeric, four digits; used with %V
%xYear for the week, where Monday is the first day of the week, numeric, four digits; used with %v
%YYear, numeric, four digits
%yYear, numeric (two digits)
%%A literal % character
%xx, for any x not listed in earlier rows