Documentation Index
Fetch the complete documentation index at: https://private-7c7dfe99-page-updates.mintlify.app/llms.txt
Use this file to discover all available pages before exploring further.
Functions for working with dates and times
Most functions in this section accept an optional time zone argument, e.g.Europe/Amsterdam. In this case, the time zone is the specified one instead of the local (default) one.
Example
For SQL standard compatibility the following functions,
NOW, CURRENT_TIMESTAMP, TODAY, and CURRENT_DATE can be used without parentheses.UTCTimestamp
Introduced in: v22.11.0 Returns the current date and time at the moment of query analysis. The function is a constant expression. This function gives the same result thatnow('UTC') would. It was added only for MySQL support. now is the preferred usage.
Syntax
UTC_timestamp
Arguments
- None.
DateTime
Examples
Get current UTC timestamp
Query
Response
YYYYMMDDToDate
Introduced in: v23.9.0 Converts a number containing the year, month and day number to aDate.
This function is the opposite of function toYYYYMMDD().
The output is undefined if the input does not encode a valid Date value.
Syntax
Date value from the provided arguments Date
Examples
Example
Query
Response
YYYYMMDDToDate32
Introduced in: v23.9.0 Converts a number containing the year, month and day number to aDate32.
This function is the opposite of function toYYYYMMDD().
The output is undefined if the input does not encode a valid Date32 value.
Syntax
Date32 value from the provided arguments Date32
Examples
Example
Query
Response
YYYYMMDDhhmmssToDateTime
Introduced in: v23.9.0 Converts a number containing the year, month, day, hour, minute, and second to aDateTime.
This function is the opposite of function toYYYYMMDDhhmmss().
The output is undefined if the input does not encode a valid DateTime value.
Syntax
YYYYMMDDhhmmss— Number containing the year, month, day, hour, minute, and second.(U)Int*orFloat*orDecimaltimezone— Timezone name.String
DateTime value from the provided arguments DateTime
Examples
Example
Query
Response
YYYYMMDDhhmmssToDateTime64
Introduced in: v23.9.0 Converts a number containing the year, month, day, hour, minute, and second to aDateTime64.
This function is the opposite of function toYYYYMMDDhhmmss().
The output is undefined if the input does not encode a valid DateTime64 value.
Syntax
YYYYMMDDhhmmss— Number containing the year, month, day, hour, minute, and second.(U)Int*orFloat*orDecimalprecision— Precision for the fractional part (0-9).UInt8timezone— Timezone name.String
DateTime64 value from the provided arguments DateTime64
Examples
Example
Query
Response
addDate
Introduced in: v23.9.0 Adds the time interval to the provided date, date with time or string-encoded date or date with time. If the addition results in a value outside the bounds of the data type, the result is undefined. Syntaxdatetime— The date or date with time to whichintervalis added.DateorDate32orDateTimeorDateTime64orStringinterval— Interval to add.Interval
interval to datetime. Date or Date32 or DateTime or DateTime64
Examples
Add interval to date
Query
Response
addDays
Introduced in: v1.1.0 Adds a specified number of days to a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to add specified number of days to.DateorDate32orDateTimeorDateTime64orStringnum— Number of days to add.(U)Int*orFloat*
datetime plus num days. Date or Date32 or DateTime or DateTime64
Examples
Add days to different date types
Query
Response
Query
Response
addHours
Introduced in: v1.1.0 Adds a specified number of hours to a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to add specified number of hours to.DateorDate32orDateTimeorDateTime64orStringnum— Number of hours to add.(U)Int*orFloat*
datetime plus num hours DateTime or DateTime64(3)
Examples
Add hours to different date types
Query
Response
Query
Response
addInterval
Introduced in: v22.11.0 Adds an interval to another interval or tuple of intervals.Intervals of the same type will be combined into a single interval. For instance if
toIntervalDay(1) and toIntervalDay(2) are passed then the result will be (3) rather than (1,1).interval_1— First interval or tuple of intervals.IntervalorTuple(Interval)interval_2— Second interval to be added.Interval
Tuple(Interval)
Examples
Add intervals
Query
Response
addMicroseconds
Introduced in: v22.6.0 Adds a specified number of microseconds to a date with time or a string-encoded date with time. Syntaxdatetime— Date with time to add specified number of microseconds to.DateTimeorDateTime64orStringnum— Number of microseconds to add.(U)Int*orFloat*
date_time plus num microseconds DateTime64
Examples
Add microseconds to different date time types
Query
Response
Query
Response
addMilliseconds
Introduced in: v22.6.0 Adds a specified number of milliseconds to a date with time or a string-encoded date with time. Syntaxdatetime— Date with time to add specified number of milliseconds to.DateTimeorDateTime64orStringnum— Number of milliseconds to add.(U)Int*orFloat*
datetime plus num milliseconds DateTime64
Examples
Add milliseconds to different date time types
Query
Response
Query
Response
addMinutes
Introduced in: v1.1.0 Adds a specified number of minutes to a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to add specified number of minutes to.DateorDate32orDateTimeorDateTime64orStringnum— Number of minutes to add.(U)Int*orFloat*
datetime plus num minutes DateTime or DateTime64(3)
Examples
Add minutes to different date types
Query
Response
Query
Response
addMonths
Introduced in: v1.1.0 Adds a specified number of months to a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to add specified number of months to.DateorDate32orDateTimeorDateTime64orStringnum— Number of months to add.(U)Int*orFloat*
datetime plus num months Date or Date32 or DateTime or DateTime64
Examples
Add months to different date types
Query
Response
Query
Response
addNanoseconds
Introduced in: v22.6.0 Adds a specified number of nanoseconds to a date with time or a string-encoded date with time. Syntaxdatetime— Date with time to add specified number of nanoseconds to.DateTimeorDateTime64orStringnum— Number of nanoseconds to add.(U)Int*orFloat*
datetime plus num nanoseconds DateTime64
Examples
Add nanoseconds to different date time types
Query
Response
Query
Response
addQuarters
Introduced in: v20.1.0 Adds a specified number of quarters to a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to add specified number of quarters to.DateorDate32orDateTimeorDateTime64orStringnum— Number of quarters to add.(U)Int*orFloat*
datetime plus num quarters Date or Date32 or DateTime or DateTime64
Examples
Add quarters to different date types
Query
Response
Query
Response
addSeconds
Introduced in: v1.1.0 Adds a specified number of seconds to a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to add specified number of seconds to.DateorDate32orDateTimeorDateTime64orStringnum— Number of seconds to add.(U)Int*orFloat*
datetime plus num seconds DateTime or DateTime64(3)
Examples
Add seconds to different date types
Query
Response
Query
Response
addTupleOfIntervals
Introduced in: v22.11.0 Consecutively adds a tuple of intervals to a date or a date with time. Syntaxdatetime— Date or date with time to add intervals to.DateorDate32orDateTimeorDateTime64intervals— Tuple of intervals to add todatetime.Tuple(Interval)
date with added intervals Date or Date32 or DateTime or DateTime64
Examples
Add tuple of intervals to date
Query
Response
addWeeks
Introduced in: v1.1.0 Adds a specified number of weeks to a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to add specified number of weeks to.DateorDate32orDateTimeorDateTime64orStringnum— Number of weeks to add.(U)Int*orFloat*
datetime plus num weeks Date or Date32 or DateTime or DateTime64
Examples
Add weeks to different date types
Query
Response
Query
Response
addYears
Introduced in: v1.1.0 Adds a specified number of years to a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to add specified number of years to.DateorDate32orDateTimeorDateTime64orStringnum— Number of years to add.(U)Int*orFloat*
datetime plus num years Date or Date32 or DateTime or DateTime64
Examples
Add years to different date types
Query
Response
Query
Response
age
Introduced in: v23.1.0 Returns the unit component of the difference betweenstartdate and enddate.
The difference is calculated using a precision of 1 nanosecond.
For example, the difference between 2021-12-29 and 2022-01-01 is 3 days for the day unit,
0 months for the month unit, and 0 years for the year unit.
For an alternative to age, see function dateDiff.
Syntax
unit— The type of interval for result.
| Unit | Possible values |
|---|---|
| nanosecond | nanosecond, nanoseconds, ns |
| microsecond | microsecond, microseconds, us, u |
| millisecond | millisecond, milliseconds, ms |
| second | second, seconds, ss, s |
| minute | minute, minutes, mi, n |
| hour | hour, hours, hh, h |
| day | day, days, dd, d |
| week | week, weeks, wk, ww |
| month | month, months, mm, m |
| quarter | quarter, quarters, qq, q |
| year | year, years, yyyy, yy |
startdate— The first time value to subtract (the subtrahend).DateorDate32orDateTimeorDateTime64enddate— The second time value to subtract from (the minuend).DateorDate32orDateTimeorDateTime64timezone— Optional. Timezone name. If specified, it is applied to both startdate and enddate. If not specified, timezones of startdate and enddate are used. If they are not the same, the result is unspecified.String
Int32
Examples
Calculate age in hours
Query
Response
Query
Response
changeDay
Introduced in: v24.7.0 Changes the day component of a date or date time. Syntaxdate_or_datetime— The value to change.DateorDate32orDateTimeorDateTime64value— The new value.(U)Int*
date_or_datetime with modified day component. Date or Date32 or DateTime or DateTime64
Examples
Usage example
Query
Response
changeHour
Introduced in: v24.7.0 Changes the hour component of a date or date time. Syntaxdate_or_datetime— The value to change.DateorDate32orDateTimeorDateTime64value— The new value.(U)Int*
date_or_datetime with modified hour component. DateTime or DateTime64
Examples
Usage example
Query
Response
changeMinute
Introduced in: v24.7.0 Changes the minute component of adate or date time.
Syntax
date_or_datetime— The value to change.DateorDate32orDateTimeorDateTime64value— The new value.(U)Int*
date_or_datetime with modified minute component. DateTime or DateTime64
Examples
Usage example
Query
Response
changeMonth
Introduced in: v24.7.0 Changes the month component of a date or date time. Syntaxdate_or_datetime— The value to change.DateorDate32orDateTimeorDateTime64value— The new value.(U)Int*
date_or_datetime with modified month component. Date or Date32 or DateTime or DateTime64
Examples
Usage example
Query
Response
changeSecond
Introduced in: v24.7.0 Changes the second component of a date or date time. Syntaxdate_or_datetime— The value to change.DateorDate32orDateTimeorDateTime64value— The new value.(U)Int*
date_or_datetime with modified seconds component. DateTime or DateTime64
Examples
Usage example
Query
Response
changeYear
Introduced in: v24.7.0 Changes the year component of a date or date time. Syntaxdate_or_datetime— The value to change.DateorDate32orDateTimeorDateTime64value— The new value.(U)Int*
date_or_datetime with modified year component. Date or Date32 or DateTime or DateTime64
Examples
Usage example
Query
Response
dateDiff
Introduced in: v23.4.0 Returns the count of the specifiedunit boundaries crossed between the startdate and the enddate.
The difference is calculated using relative units. For example, the difference between 2021-12-29 and 2022-01-01 is 3 days for unit day
(see toRelativeDayNum), 1 month for unit month (see toRelativeMonthNum) and 1 year for unit year
(see toRelativeYearNum).
If the unit week was specified, then dateDiff assumes that weeks start on Monday.
Note that this behavior is different from that of function toWeek() in which weeks start by default on Sunday.
For an alternative to dateDiff, see function age.
Syntax
timestampDiff, DATE_DIFF, date_diff, TIMESTAMP_DIFF, timestamp_diff
Arguments
unit— The type of interval for result.
| Unit | Possible values |
|---|---|
| nanosecond | nanosecond, nanoseconds, ns |
| microsecond | microsecond, microseconds, us, u |
| millisecond | millisecond, milliseconds, ms |
| second | second, seconds, ss, s |
| minute | minute, minutes, mi, n |
| hour | hour, hours, hh, h |
| day | day, days, dd, d |
| week | week, weeks, wk, ww |
| month | month, months, mm, m |
| quarter | quarter, quarters, qq, q |
| year | year, years, yyyy, yy |
startdate— The first time value to subtract (the subtrahend).DateorDate32orDateTimeorDateTime64enddate— The second time value to subtract from (the minuend).DateorDate32orDateTimeorDateTime64timezone— Optional. Timezone name. If specified, it is applied to bothstartdateandenddate. If not specified, timezones ofstartdateandenddateare used. If they are not the same, the result is unspecified.String
enddate and startdate expressed in unit. Int64
Examples
Calculate date difference in hours
Query
Response
Query
Response
dateName
Introduced in: v21.7.0 Returns the specified part of the date. Possible values:- ‘year’
- ‘quarter’
- ‘month’
- ‘week’
- ‘dayofyear’
- ‘day’
- ‘weekday’
- ‘hour’
- ‘minute’
- ‘second’
date_part— The part of the date that you want to extract.Stringdatetime— A date or date with time value.DateorDate32orDateTimeorDateTime64timezone— Optional. Timezone.String
String
Examples
Extract different date parts
Query
Response
dateTrunc
Introduced in: v20.8.0 Truncates a date and time value to the specified part of the date. SyntaxDATE_TRUNC
Arguments
unit— The type of interval to truncate the result. Possible values:nanosecond(only DateTime64),microsecond(only DateTime64),millisecond(only DateTime64),second,minute,hour,day,week,month,quarter,year.Stringdatetime— Date and time.DateorDate32orDateTimeorDateTime64timezone— Optional. Timezone name for the returned datetime. If not specified, the function uses the timezone of thedatetimeparameter.String
| Unit Argument | datetime Argument | Return Type |
|---|---|---|
| Year, Quarter, Month, Week | Date32 or DateTime64 or Date or DateTime | Date32 or Date |
| Day, Hour, Minute, Second | Date32, DateTime64, Date, or DateTime | DateTime64 or DateTime |
| Millisecond, Microsecond, | Any | DateTime64 |
| Nanosecond | with scale 3, 6, or 9 |
Query
Response
Query
Response
formatDateTime
Introduced in: v1.1.0 Formats a date or date with time according to the given format string.format is a constant expression, so you cannot have multiple formats for a single result column.
formatDateTime uses MySQL datetime format style, refer to the mysql docs.
The opposite operation of this function is parseDateTime.
Using replacement fields, you can define a pattern for the resulting string.
The example column in the table below shows formatting result for 2018-01-02 22:33:44.
Replacement fields:
| Placeholder | Description | Example |
|---|---|---|
| %a | abbreviated weekday name (Mon-Sun) | Mon |
| %b | abbreviated month name (Jan-Dec) | Jan |
| %c | month as an integer number (01-12) | 01 |
| %C | year divided by 100 and truncated to integer (00-99) | 20 |
| %d | day of the month, zero-padded (01-31) | 02 |
| %D | Short MM/DD/YY date, equivalent to %m/%d/%y | 01/02/18 |
| %e | day of the month, space-padded (1-31) | 2 |
| %f | fractional second | 123456 |
| %F | short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2018-01-02 |
| %g | two-digit year format, aligned to ISO 8601 | 18 |
| %G | four-digit year format for ISO week number | 2018 |
| %h | hour in 12h format (01-12) | 09 |
| %H | hour in 24h format (00-23) | 22 |
| %i | minute (00-59) | 33 |
| %I | hour in 12h format (01-12) | 10 |
| %j | day of the year (001-366) | 002 |
| %k | hour in 24h format (00-23) | 14 |
| %l | hour in 12h format (01-12) | 09 |
| %m | month as an integer number (01-12) | 01 |
| %M | full month name (January-December) | January |
| %n | new-line character | |
| %p | AM or PM designation | PM |
| %Q | Quarter (1-4) | 1 |
| %r | 12-hour HH:MM AM/PM time, equivalent to %h:%i %p | 10:30 PM |
| %R | 24-hour HH:MM time, equivalent to %H:%i | 22:33 |
| %s | second (00-59) | 44 |
| %S | second (00-59) | 44 |
| %t | horizontal-tab character | |
| %T | ISO 8601 time format (HH:MM:SS), equivalent to %H:%i:%S | 22:33:44 |
| %u | ISO 8601 weekday as number with Monday as 1 (1-7) | 2 |
| %V | ISO 8601 week number (01-53) | 01 |
| %w | weekday as a integer number with Sunday as 0 (0-6) | 2 |
| %W | full weekday name (Monday-Sunday) | Monday |
| %y | Year, last two digits (00-99) | 18 |
| %Y | Year | 2018 |
| %z | Time offset from UTC as +HHMM or -HHMM | -0500 |
| %% | a % sign | % |
- In ClickHouse versions earlier than v23.4,
%fprints a single zero (0) if the formatted value is a Date, Date32 or DateTime (which have no fractional seconds) or a DateTime64 with a precision of 0. - In ClickHouse versions earlier than v25.1,
%fprints as many digits as specified by the scale of the DateTime64 instead of fixed 6 digits. - In ClickHouse versions earlier than v23.4,
%Mprints the minute (00-59) instead of the full month name (January-December).
DATE_FORMAT
Arguments
datetime— A date or date time to format.DateorDate32orDateTimeorDateTime64format— Format string with replacement fields.Stringtimezone— Optional. Timezone name for the formatted time.String
String
Examples
Format date with year placeholder
Query
Response
Query
Response
Query
Response
formatDateTimeInJodaSyntax
Introduced in: v20.1.0 Similar toformatDateTime, except that it formats datetime in Joda style instead of MySQL style. Refer to Joda Time documentation.
The opposite operation of this function is parseDateTimeInJodaSyntax.
Using replacement fields, you can define a pattern for the resulting string.
Replacement fields:
| Placeholder | Description | Presentation | Examples |
|---|---|---|---|
| G | era | text | AD |
| C | century of era (>=0) | number | 20 |
| Y | year of era (>=0) | year | 1996 |
| x | weekyear (not supported yet) | year | 1996 |
| w | week of weekyear (not supported yet) | number | 27 |
| e | day of week | number | 2 |
| E | day of week | text | Tuesday; Tue |
| y | year | year | 1996 |
| D | day of year | number | 189 |
| M | month of year | month | July; Jul; 07 |
| d | day of month | number | 10 |
| a | halfday of day | text | PM |
| K | hour of halfday (0~11) | number | 0 |
| h | clockhour of halfday (1~12) | number | 12 |
| H | hour of day (0~23) | number | 0 |
| k | clockhour of day (1~24) | number | 24 |
| m | minute of hour | number | 30 |
| s | second of minute | number | 55 |
| S | fraction of second | number | 978 |
| z | time zone | text | Eastern Standard Time; EST |
| Z | time zone offset | zone | -0800; -0812 |
| ’ | escape for text | delimiter | |
| ” | single quote | literal | ’ |
datetime— A date or date time to format.DateTimeorDateorDate32orDateTime64format— Format string with Joda-style replacement fields.Stringtimezone— Optional. Timezone name for the formatted time.String
String
Examples
Format datetime using Joda syntax
Query
Response
fromDaysSinceYearZero
Introduced in: v23.11.0 For a given number of days elapsed since 1 January 0000, returns the corresponding date in the proleptic Gregorian calendar defined by ISO 8601. The calculation is the same as in MySQL’sFROM_DAYS() function. The result is undefined if it cannot be represented within the bounds of the Date type.
Syntax
FROM_DAYS
Arguments
days— The number of days passed since year zero.UInt32
Date
Examples
Convert days since year zero to dates
Query
Response
fromDaysSinceYearZero32
Introduced in: v23.11.0 For a given number of days elapsed since 1 January 0000, returns the corresponding date in the proleptic Gregorian calendar defined by ISO 8601. The calculation is the same as in MySQL’sFROM_DAYS() function. The result is undefined if it cannot be represented within the bounds of the Date32 type.
Syntax
days— The number of days passed since year zero.UInt32
Date32
Examples
Convert days since year zero to dates
Query
Response
fromModifiedJulianDay
Introduced in: v21.1.0 Converts a Modified Julian Day number to a Proleptic Gregorian calendar date in text formYYYY-MM-DD. This function supports day number from -678941 to 2973483 (which represent 0000-01-01 and 9999-12-31 respectively). It raises an exception if the day number is outside of the supported range.
Syntax
day— Modified Julian Day number.(U)Int*
String
Examples
Convert Modified Julian Day to date
Query
Response
fromModifiedJulianDayOrNull
Introduced in: v21.1.0 Similar tofromModifiedJulianDay(), but instead of raising exceptions it returns NULL.
Syntax
day— Modified Julian Day number.(U)Int*
day argument, otherwise null. Nullable(String)
Examples
Convert Modified Julian Day to date with null handling
Query
Response
fromUTCTimestamp
Introduced in: v22.1.0 Converts a date or date with time value from UTC timezone to a date or date with time value with the specified time zone. This function is mainly included for compatibility with Apache Spark and similar frameworks. Syntaxfrom_utc_timestamp
Arguments
datetime— A date or date with time const value or an expression.DateTimeorDateTime64time_zone— A String type const value or an expression representing the time zone.String
DateTime or DateTime64
Examples
Convert UTC timezone to specified timezone
Query
Response
fromUnixTimestamp
Introduced in: v20.8.0 This function converts a Unix timestamp to a calendar date and a time of a day. It can be called in two ways:- When given a single argument of type
Integer, it returns a value of typeDateTime, i.e. behaves liketoDateTime. - When given two or three arguments where the first argument is a value of type
Integer,Date,Date32,DateTimeorDateTime64, the second argument is a constant format string and the third argument is an optional constant time zone string, the function returns a value of typeString, i.e. it behaves likeformatDateTime. In this case, MySQL’s datetime format style is used.
FROM_UNIXTIME
Arguments
timestamp— Unix timestamp or date/date with time value.(U)Int*orDateorDate32orDateTimeorDateTime64format— Optional. Constant format string for output formatting.Stringtimezone— Optional. Constant time zone string.String
DateTime of the timestamp when called with one argument, or a String when called with two or three arguments. DateTime or String
Examples
Convert Unix timestamp to DateTime
Query
Response
Query
Response
fromUnixTimestampInJodaSyntax
Introduced in: v23.1.0 This function converts a Unix timestamp to a calendar date and a time of a day. It can be called in two ways: When given a single argument of typeInteger, it returns a value of type DateTime, i.e. behaves like toDateTime.
When given two or three arguments where the first argument is a value of type Integer, Date, Date32, DateTime or DateTime64, the second argument is a constant format string and the third argument is an optional constant time zone string, the function returns a value of type String, i.e. it behaves like formatDateTimeInJodaSyntax. In this case, Joda datetime format style is used.
Syntax
timestamp— Unix timestamp or date/time value.(U)Int*orDateorDate32orDateTimeorDateTime64format— Optional. Constant format string using Joda syntax for output formatting.Stringtimezone— Optional. Constant time zone string.String
DateTime or String
Examples
Convert Unix timestamp with Joda format
Query
Response
makeDate
Introduced in: v22.6.0 Creates aDate from either:
- a year, month and day
- a year and day of year
year— Year number.(U)Int*orFloat*orDecimalmonth— Month number (1-12).(U)Int*orFloat*orDecimalday— Day of the month (1-31).(U)Int*orFloat*orDecimalday_of_year— Day of the year (1-365).(U)Int*orFloat*orDecimal
Date value constructed from the provided arguments Date
Examples
Date from a year, month, day
Query
Response
Query
Response
makeDate32
Introduced in: v22.6.0 Creates aDate32 from either:
- a year, month and day
- a year and day of year
year— Year number.(U)Int*orFloat*orDecimalmonth— Month number (1-12).(U)Int*orFloat*orDecimalday— Day of the month (1-31).(U)Int*orFloat*orDecimalday_of_year— Day of the year (1-365).(U)Int*orFloat*orDecimal
Date32 value constructed from the provided arguments Date32
Examples
Date32 from a year, month, day
Query
Response
Query
Response
makeDateTime
Introduced in: v22.6.0 Creates aDateTime from year, month, day, hour, minute, and second, with optional timezone.
Syntax
year— Year number.(U)Int*orFloat*orDecimalmonth— Month number (1-12).(U)Int*orFloat*orDecimalday— Day of the month (1-31).(U)Int*orFloat*orDecimalhour— Hour (0-23).(U)Int*orFloat*orDecimalminute— Minute (0-59).(U)Int*orFloat*orDecimalsecond— Second (0-59).(U)Int*orFloat*orDecimaltimezone— Timezone name.String
DateTime value constructed from the provided arguments DateTime
Examples
DateTime from year, month, day, hour, minute, second
Query
Response
makeDateTime64
Introduced in: v22.6.0 Creates aDateTime64 from year, month, day, hour, minute, second, with optional fraction, precision, and timezone.
Syntax
year— Year number.(U)Int*orFloat*orDecimalmonth— Month number (1-12).(U)Int*orFloat*orDecimalday— Day of the month (1-31).(U)Int*orFloat*orDecimalhour— Hour (0-23).(U)Int*orFloat*orDecimalminute— Minute (0-59).(U)Int*orFloat*orDecimalsecond— Second (0-59).(U)Int*orFloat*orDecimalfraction— Fractional part of the second.(U)Int*orFloat*orDecimalprecision— Precision for the fractional part (0-9).UInt8timezone— Timezone name.String
DateTime64 value constructed from the provided arguments DateTime64
Examples
DateTime64 from year, month, day, hour, minute, second
Query
Response
monthName
Introduced in: v22.1.0 Returns the name of the month as a string from a date or date with time value. Syntaxdatetime— Date or date with time.DateorDate32orDateTimeorDateTime64
String
Examples
Get month name from date
Query
Response
now
Introduced in: v1.1.0 Returns the current date and time at the moment of query analysis. The function is a constant expression. Syntaxcurrent_timestamp
Arguments
timezone— Optional. Timezone name for the returned value.String
DateTime
Examples
Query without timezone
Query
Response
Query
Response
Query
Response
now64
Introduced in: v20.1.0 Returns the current date and time with sub-second precision at the moment of query analysis. The function is a constant expression. Syntaxscale— Optional. Tick size (precision): 10^-precision seconds. Valid range: [0 : 9]. Typically, are used - 3 (default) (milliseconds), 6 (microseconds), 9 (nanoseconds).UInt8timezone— Optional. Timezone name for the returned value.String
DateTime64
Examples
Query with default and custom precision
Query
Response
nowInBlock
Introduced in: v22.8.0 Returns the current date and time at the moment of processing of each block of data. In contrast to the functionnow, it is not a constant expression, and the returned value will be different in different blocks for long-running queries.
It makes sense to use this function to generate the current time in long-running INSERT SELECT queries.
Syntax
timezone— Optional. Timezone name for the returned value.String
DateTime
Examples
Difference with the now() function
Query
Response
nowInBlock64
Introduced in: v25.8.0 Returns the current date and time at the moment of processing of each block of data in milliseconds. In contrast to the function now64, it is not a constant expression, and the returned value will be different in different blocks for long-running queries. It makes sense to use this function to generate the current time in long-running INSERT SELECT queries. Syntaxscale— Optional. Tick size (precision): 10^-precision seconds. Valid range: [0 : 9]. Typically, are used - 3 (default) (milliseconds), 6 (microseconds), 9 (nanoseconds).UInt8timezone— Optional. Timezone name for the returned value.String
DateTime64
Examples
Difference with the now64() function
Query
Response
serverTimezone
Introduced in: v23.6.0 Returns the timezone of the server, i.e. the value of thetimezone setting.
If the function is executed in the context of a distributed table, then it generates a normal column with values relevant to each shard. Otherwise, it produces a constant value.
Syntax
serverTimeZone
Arguments
- None.
String
Examples
Usage example
Query
Response
subDate
Introduced in: v23.9.0 Subtracts the time interval from the provided date, date with time or string-encoded date or date with time. If the subtraction results in a value outside the bounds of the data type, the result is undefined. Syntaxdatetime— The date or date with time from whichintervalis subtracted.DateorDate32orDateTimeorDateTime64interval— Interval to subtract.Interval
interval from datetime. Date or Date32 or DateTime or DateTime64
Examples
Subtract interval from date
Query
Response
subtractDays
Introduced in: v1.1.0 Subtracts a specified number of days from a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to subtract specified number of days from.DateorDate32orDateTimeorDateTime64orStringnum— Number of days to subtract.(U)Int*orFloat*
datetime minus num days Date or Date32 or DateTime or DateTime64
Examples
Subtract days from different date types
Query
Response
Query
Response
subtractHours
Introduced in: v1.1.0 Subtracts a specified number of hours from a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to subtract specified number of hours from.DateorDate32orDateTimeorDateTime64orStringnum— Number of hours to subtract.(U)Int*orFloat*
datetime minus num hours DateTime or DateTime64(3)
Examples
Subtract hours from different date types
Query
Response
Query
Response
subtractInterval
Introduced in: v22.11.0 Adds a negated interval to another interval or tuple of intervals. Note: Intervals of the same type will be combined into a single interval. For instance iftoIntervalDay(2) and toIntervalDay(1) are
passed then the result will be (1) rather than (2,1).
Syntax
interval_1— First interval or interval of tuples.IntervalorTuple(Interval)interval_2— Second interval to be negated.Interval
Tuple(T)
Examples
Subtract intervals
Query
Response
subtractMicroseconds
Introduced in: v22.6.0 Subtracts a specified number of microseconds from a date with time or a string-encoded date with time. Syntaxdatetime— Date with time to subtract specified number of microseconds from.DateTimeorDateTime64orStringnum— Number of microseconds to subtract.(U)Int*orFloat*
datetime minus num microseconds DateTime64
Examples
Subtract microseconds from different date time types
Query
Response
Query
Response
subtractMilliseconds
Introduced in: v22.6.0 Subtracts a specified number of milliseconds from a date with time or a string-encoded date with time. Syntaxdatetime— Date with time to subtract specified number of milliseconds from.DateTimeorDateTime64orStringnum— Number of milliseconds to subtract.(U)Int*orFloat*
datetime minus num milliseconds DateTime64
Examples
Subtract milliseconds from different date time types
Query
Response
Query
Response
subtractMinutes
Introduced in: v1.1.0 Subtracts a specified number of minutes from a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to subtract specified number of minutes from.DateorDate32orDateTimeorDateTime64orStringnum— Number of minutes to subtract.(U)Int*orFloat*
datetime minus num minutes DateTime or DateTime64(3)
Examples
Subtract minutes from different date types
Query
Response
Query
Response
subtractMonths
Introduced in: v1.1.0 Subtracts a specified number of months from a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to subtract specified number of months from.DateorDate32orDateTimeorDateTime64orStringnum— Number of months to subtract.(U)Int*orFloat*
datetime minus num months Date or Date32 or DateTime or DateTime64
Examples
Subtract months from different date types
Query
Response
Query
Response
subtractNanoseconds
Introduced in: v20.1.0 Subtracts a specified number of nanoseconds from a date with time or a string-encoded date with time. Syntaxdatetime— Date with time to subtract specified number of nanoseconds from.DateTimeorDateTime64orStringnum— Number of nanoseconds to subtract.(U)Int*orFloat*
datetime minus num nanoseconds DateTime64
Examples
Subtract nanoseconds from different date time types
Query
Response
Query
Response
subtractQuarters
Introduced in: v20.1.0 Subtracts a specified number of quarters from a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to subtract specified number of quarters from.DateorDate32orDateTimeorDateTime64orStringnum— Number of quarters to subtract.(U)Int*orFloat*
datetime minus num quarters Date or Date32 or DateTime or DateTime64
Examples
Subtract quarters from different date types
Query
Response
Query
Response
subtractSeconds
Introduced in: v1.1.0 Subtracts a specified number of seconds from a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to subtract specified number of seconds from.DateorDate32orDateTimeorDateTime64orStringnum— Number of seconds to subtract.(U)Int*orFloat*
datetime minus num seconds DateTime or DateTime64(3)
Examples
Subtract seconds from different date types
Query
Response
Query
Response
subtractTupleOfIntervals
Introduced in: v22.11.0 Consecutively subtracts a tuple of intervals from a date or a date with time. Syntaxdatetime— Date or date with time to subtract intervals from.DateorDate32orDateTimeorDateTime64intervals— Tuple of intervals to subtract fromdatetime.Tuple(Interval)
date with subtracted intervals Date or Date32 or DateTime or DateTime64
Examples
Subtract tuple of intervals from date
Query
Response
subtractWeeks
Introduced in: v1.1.0 Subtracts a specified number of weeks from a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to subtract specified number of weeks from.DateorDate32orDateTimeorDateTime64orStringnum— Number of weeks to subtract.(U)Int*orFloat*
datetime minus num weeks Date or Date32 or DateTime or DateTime64
Examples
Subtract weeks from different date types
Query
Response
Query
Response
subtractYears
Introduced in: v1.1.0 Subtracts a specified number of years from a date, a date with time or a string-encoded date or date with time. Syntaxdatetime— Date or date with time to subtract specified number of years from.DateorDate32orDateTimeorDateTime64orStringnum— Number of years to subtract.(U)Int*orFloat*
datetime minus num years Date or Date32 or DateTime or DateTime64
Examples
Subtract years from different date types
Query
Response
Query
Response
timeDiff
Introduced in: v23.4.0 Returns the difference between two dates or dates with time values in seconds. The difference is calculated asenddate - startdate.
This function is equivalent to dateDiff('second', startdate, enddate).
For calculating time differences in other units (hours, days, months, etc.), use the dateDiff function instead.
Syntax
startdate— The first time value to subtract (the subtrahend).DateorDate32orDateTimeorDateTime64enddate— The second time value to subtract from (the minuend).DateorDate32orDateTimeorDateTime64
enddate and startdate expressed in seconds. Int64
Examples
Calculate time difference in seconds
Query
Response
Query
Response
Query
Response
timeSlot
Introduced in: v1.1.0 Round the time to the start of a half-an-hour length interval.Although this function can take values of the extended types
Date32 and DateTime64 as an argument,
passing it a time outside the normal range (year 1970 to 2149 for Date / 2106 for DateTime) will produce wrong results.time— Time to round to the start of a half-an-hour length interval.DateTimeorDate32orDateTime64time_zone— Optional. A String type const value or an expression representing the time zone.String
DateTime
Examples
Round time to half-hour interval
Query
Response
timeSlots
Introduced in: v1.1.0 For a time interval starting atStartTime and continuing for Duration seconds, it returns an array of moments in time, consisting of points from this interval rounded down to the Size in seconds. Size is an optional parameter set to 1800 (30 minutes) by default.
This is necessary, for example, when searching for pageviews in the corresponding session.
For DateTime64, the return value’s scale can differ from the scale of StartTime. The highest scale among all given arguments is taken.
Syntax
StartTime— Starting time for the interval.DateTimeorDateTime64Duration— Duration of the interval in seconds.UInt32orDateTime64Size— Optional. Size of time slots in seconds. Default is 1800 (30 minutes).UInt32orDateTime64
StartTime). For DateTime64, the return value’s scale can differ from the scale of StartTime - the highest scale among all given arguments is taken. Array(DateTime) or Array(DateTime64)
Examples
Generate time slots for an interval
Query
Response
timestamp
Introduced in: v23.9.0 Converts the first argumentexpr to type DateTime64(6).
If a second argument expr_time is provided, it adds the specified time to the converted value.
Syntax
expr— Date or date with time.Stringexpr_time— Optional. Time to add to the converted value.String
expr, or expr with added time DateTime64(6)
Examples
Convert date string to DateTime64(6)
Query
Response
Query
Response
timezone
Introduced in: v21.4.0 Returns the time zone name of the current session or converts a time zone offset or name to a canonical time zone name. SyntaxtimeZone
Arguments
- None.
String
Examples
Usage example
Query
Response
timezoneOf
Introduced in: v21.4.0 Returns the timezone name of aDateTime or DateTime64 value.
Syntax
timeZoneOf
Arguments
datetime— A value of type.DateTimeorDateTime64timezone— Optional. Timezone name to convert thedatetimevalue’s timezone to.String
datetime String
Examples
Usage example
Query
Response
timezoneOffset
Introduced in: v21.6.0 Returns the timezone offset in seconds from UTC. The function takes daylight saving time and historical timezone changes at the specified date and time into account. SyntaxtimeZoneOffset
Arguments
datetime—DateTimevalue to get the timezone offset for.DateTimeorDateTime64
Int32
Examples
Usage example
Query
Response
toDayOfMonth
Introduced in: v1.1.0 Returns the day of the month (1-31) of aDate or DateTime.
Syntax
DAY, DAYOFMONTH
Arguments
datetime— Date or date with time to get the day of month from.DateorDate32orDateTimeorDateTime64
UInt8
Examples
Usage example
Query
Response
toDayOfWeek
Introduced in: v1.1.0 Returns the number of the day within the week of aDate or DateTime value.
The two-argument form of toDayOfWeek() enables you to specify whether the week starts on Monday or Sunday,
and whether the return value should be in the range from 0 to 6 or 1 to 7.
| Mode | First day of week | Range |
|---|---|---|
| 0 | Monday | 1-7: Monday = 1, Tuesday = 2, …, Sunday = 7 |
| 1 | Monday | 0-6: Monday = 0, Tuesday = 1, …, Sunday = 6 |
| 2 | Sunday | 0-6: Sunday = 0, Monday = 1, …, Saturday = 6 |
| 3 | Sunday | 1-7: Sunday = 1, Monday = 2, …, Saturday = 7 |
DAYOFWEEK
Arguments
datetime— Date or date with time to get the day of week from.DateorDate32orDateTimeorDateTime64mode— Optional. Integer specifying the week mode (0-3). Defaults to 0 if omitted.UInt8timezone— Optional. Timezone to use for the conversion.String
Date or DateTime UInt8
Examples
Usage example
Query
Response
toDayOfYear
Introduced in: v18.4.0 Returns the number of the day within the year (1-366) of aDate or DateTime value.
Syntax
DAYOFYEAR
Arguments
datetime— Date or date with time to get the day of year from.DateorDate32orDateTimeorDateTime64
UInt16
Examples
Usage example
Query
Response
toDaysInMonth
Introduced in: v26.3.0 Returns the number of days in the month of aDate or DateTime.
The returned value is in the range 28 to 31.
Syntax
datetime— Date or date with time to get the number of days in the month from.DateorDate32orDateTimeorDateTime64
UInt8
Examples
Usage example
Query
Response
toDaysSinceYearZero
Introduced in: v23.9.0 For a given date, returns the number of days which have passed since 1 January 0000 in the proleptic Gregorian calendar defined by ISO 8601. The calculation is the same as in MySQL’sTO_DAYS function.
Syntax
TO_DAYS
Arguments
date— The date or date with time for which to calculate the number of days since year zero from.DateorDate32orDateTimeorDateTime64time_zone— Time zone.String
0000-01-01. UInt32
Examples
Calculate days since year zero
Query
Response
toHour
Introduced in: v1.1.0 Returns the hour component (0-23) of aDateTime or DateTime64 value.
Syntax
HOUR
Arguments
datetime— Date with time to get the hour from.DateTimeorDateTime64
datetime. UInt8
Examples
Usage example
Query
Response
toISOWeek
Introduced in: v20.1.0 Returns the ISO week number of a date or date with time. This is a compatibility function that is equivalent totoWeek(date, 3).
ISO weeks start on Monday and the first week of the year contains January 4th.
According to ISO 8601, week numbers are in the range from 1 to 53.
Note that dates near the beginning or end of a year may return a week number from the previous or next year. For example,
December 29, 2025 returns week 1 because it falls in the first week that contains January 4, 2026.
Syntax
datetime— Date or date with time to get the ISO week number from.DateorDateTimeorDate32orDateTime64timezone— Optional. Time zone.String
UInt8
Examples
Get ISO week numbers
Query
Response
Query
Response
toISOYear
Introduced in: v18.4.0 Converts a date or date with time to the ISO year number. Syntaxdatetime— The value with date or date with time.DateorDate32orDateTimeorDateTime64
UInt16
Examples
Get ISO year from date values
Query
Response
toLastDayOfMonth
Introduced in: v1.1.0 Rounds up a date or date with time to the last day of the month.The return type can be configured by setting
enable_extended_results_for_datetime_functions.LAST_DAY
Arguments
value— The date or date with time to round up to the last day of the month.DateorDate32orDateTimeorDateTime64
Date
Examples
Round up to the last day of the month
Query
Response
toLastDayOfWeek
Introduced in: v23.5.0 Rounds a date or date with time up to the nearest Saturday or Sunday.The return type can be configured by setting
enable_extended_results_for_datetime_functions.datetime— A date or date with time to convert.DateorDateTimeorDate32orDateTime64mode— Determines the first day of the week as described in thetoWeek()function. Default0.UInt8timezone— Optional. The timezone to use for the conversion. If not specified, the server’s timezone is used.String
Date or Date32
Examples
Round up to the nearest Saturday or Sunday
Query
Response
toMillisecond
Introduced in: v24.2.0 Returns the millisecond component (0-999) of aDateTime or DateTime64 value.
Syntax
MILLISECOND
Arguments
datetime— Date with time to get the millisecond from.DateTimeorDateTime64
datetime. UInt16
Examples
Usage example
Query
Response
toMinute
Introduced in: v1.1.0 Returns the minute component (0-59) of aDate or DateTime value.
Syntax
MINUTE
Arguments
datetime— Date with time to get the minute from.DateTimeorDateTime64
datetime. UInt8
Examples
Usage example
Query
Response
toModifiedJulianDay
Introduced in: v21.1.0 Converts a Proleptic Gregorian calendar date in text formYYYY-MM-DD to a Modified Julian Day number in Int32. This function supports date from 0000-01-01 to 9999-12-31. It raises an exception if the argument cannot be parsed as a date, or the date is invalid.
Syntax
date— The date in String form.StringorFixedString
Int32
Examples
Convert date to Modified Julian Day
Query
Response
toModifiedJulianDayOrNull
Introduced in: v21.1.0 Similar totoModifiedJulianDay(), but instead of raising exceptions it returns NULL.
Syntax
date— Date in text form.StringorFixedString
date, otherwise null. Nullable(Int32)
Examples
Convert date to Modified Julian Day with null handling
Query
Response
toMonday
Introduced in: v1.1.0 Rounds down a date or date with time to the Monday of the same week. Returns the date.The return type can be configured by setting
enable_extended_results_for_datetime_functions.value— Date or date with time to round down to the Monday of the week.DateorDate32orDateTimeorDateTime64
Date
Examples
Round down to the Monday of the week
Query
Response
toMonth
Introduced in: v1.1.0 Returns the month component (1-12) of aDate or DateTime value.
Syntax
MONTH
Arguments
datetime— Date or date with time to get the month from.DateorDate32orDateTimeorDateTime64
UInt8
Examples
Usage example
Query
Response
toMonthNumSinceEpoch
Introduced in: v25.3.0 Returns amount of months passed from year 1970 Syntaxdate— A date or date with time.DateorDateTimeorDateTime64
Query
Response
toQuarter
Introduced in: v1.1.0 Returns the quarter of the year (1-4) for a givenDate or DateTime value.
Syntax
QUARTER
Arguments
datetime— Date or date with time to get the quarter of the year from.DateorDate32orDateTimeorDateTime64
UInt8
Examples
Usage example
Query
Response
toRelativeDayNum
Introduced in: v1.1.0 Converts a date or date with time to the number of days elapsed since a certain fixed point in the past. The exact point in time is an implementation detail, and therefore this function is not intended to be used standalone. The main purpose of the function is to calculate the difference in days between two dates or dates with time, e.g.,toRelativeDayNum(dt1) - toRelativeDayNum(dt2).
Syntax
date— Date or date with time.DateorDateTimeorDateTime64
UInt32
Examples
Get relative day numbers
Query
Response
toRelativeHourNum
Introduced in: v1.1.0 Converts a date or date with time to the number of hours elapsed since a certain fixed point in the past. The exact point in time is an implementation detail, and therefore this function is not intended to be used standalone. The main purpose of the function is to calculate the difference in hours between two dates or dates with time, e.g.,toRelativeHourNum(dt1) - toRelativeHourNum(dt2).
Syntax
date— Date or date with time.DateorDateTimeorDateTime64
UInt32
Examples
Get relative hour numbers
Query
Response
toRelativeMinuteNum
Introduced in: v1.1.0 Converts a date or date with time to the number of minutes elapsed since a certain fixed point in the past. The exact point in time is an implementation detail, and therefore this function is not intended to be used standalone. The main purpose of the function is to calculate the difference in minutes between two dates or dates with time, e.g.,toRelativeMinuteNum(dt1) - toRelativeMinuteNum(dt2).
Syntax
date— Date or date with time.DateorDateTimeorDateTime64
UInt32
Examples
Get relative minute numbers
Query
Response
toRelativeMonthNum
Introduced in: v1.1.0 Converts a date or date with time to the number of months elapsed since a certain fixed point in the past. The exact point in time is an implementation detail, and therefore this function is not intended to be used standalone. The main purpose of the function is to calculate the difference in months between two dates or dates with time, e.g.,toRelativeMonthNum(dt1) - toRelativeMonthNum(dt2).
Syntax
date— Date or date with time.DateorDateTimeorDateTime64
UInt32
Examples
Get relative month numbers
Query
Response
toRelativeQuarterNum
Introduced in: v1.1.0 Converts a date or date with time to the number of quarters elapsed since a certain fixed point in the past. The exact point in time is an implementation detail, and therefore this function is not intended to be used standalone. The main purpose of the function is to calculate the difference in quarters between two dates or dates with time, e.g.,toRelativeQuarterNum(dt1) - toRelativeQuarterNum(dt2).
Syntax
date— Date or date with time.DateorDateTimeorDateTime64
UInt32
Examples
Get relative quarter numbers
Query
Response
toRelativeSecondNum
Introduced in: v1.1.0 Converts a date or date with time to the number of seconds elapsed since a certain fixed point in the past. The exact point in time is an implementation detail, and therefore this function is not intended to be used standalone. The main purpose of the function is to calculate the difference in seconds between two dates or dates with time, e.g.,toRelativeSecondNum(dt1) - toRelativeSecondNum(dt2).
Syntax
date— Date or date with time.DateorDateTimeorDateTime64
UInt32
Examples
Get relative second numbers
Query
Response
toRelativeWeekNum
Introduced in: v1.1.0 Converts a date or date with time to the number of weeks elapsed since a certain fixed point in the past. The exact point in time is an implementation detail, and therefore this function is not intended to be used standalone. The main purpose of the function is to calculate the difference in weeks between two dates or dates with time, e.g.,toRelativeWeekNum(dt1) - toRelativeWeekNum(dt2).
Syntax
date— Date or date with time.DateorDateTimeorDateTime64
UInt32
Examples
Get relative week numbers
Query
Response
toRelativeYearNum
Introduced in: v1.1.0 Converts a date or date with time to the number of years elapsed since a certain fixed point in the past. The exact point in time is an implementation detail, and therefore this function is not intended to be used standalone. The main purpose of the function is to calculate the difference in years between two dates or dates with time, e.g.,toRelativeYearNum(dt1) - toRelativeYearNum(dt2).
Syntax
date— Date or date with time.DateorDateTimeorDateTime64
UInt16
Examples
Get relative year numbers
Query
Response
toSecond
Introduced in: v1.1.0 Returns the second component (0-59) of aDateTime or DateTime64 value.
Syntax
SECOND
Arguments
datetime— Date with time to get the second from.DateTimeorDateTime64
datetime. UInt8
Examples
Usage example
Query
Response
toStartOfDay
Introduced in: v1.1.0 Rounds down a date with time to the start of the day.The return type can be configured by setting
enable_extended_results_for_datetime_functions.DateTime or DateTime64
Examples
Round down to the start of the day
Query
Response
toStartOfFifteenMinutes
Introduced in: v1.1.0 Rounds down the date with time to the start of the fifteen-minute interval.The return type can be configured by setting
enable_extended_results_for_datetime_functions.datetime— A date or date with time to round.DateTimeorDateTime64
DateTime or DateTime64
Examples
Example
Query
Response
toStartOfFiveMinutes
Introduced in: v22.6.0 Rounds down a date with time to the start of the nearest five-minute interval.The return type can be configured by setting
enable_extended_results_for_datetime_functions.toStartOfFiveMinute
Arguments
datetime— A date with time to round.DateTimeorDateTime64
DateTime or DateTime64
Examples
Example
Query
Response
toStartOfHour
Introduced in: v1.1.0 Rounds down a date with time to the start of the hour.The return type can be configured by setting
enable_extended_results_for_datetime_functions.datetime— A date with time to round.DateTimeorDateTime64
DateTime or DateTime64
Examples
Round down to the start of the hour
Query
Response
toStartOfISOYear
Introduced in: v1.1.0 Rounds down a date or date with time to the first day of the ISO year, which can be different than a regular year. See ISO week date.The return type can be configured by setting
enable_extended_results_for_datetime_functions.value— The date or date with time to round down to the first day of the ISO year.DateorDate32orDateTimeorDateTime64
Date
Examples
Round down to the first day of the ISO year
Query
Response
toStartOfInterval
Introduced in: v20.1.0 This function generalizes othertoStartOf*() functions with toStartOfInterval(date_or_date_with_time, INTERVAL x unit [, time_zone]) syntax.
For example,
toStartOfInterval(t, INTERVAL 1 YEAR)returns the same astoStartOfYear(t),toStartOfInterval(t, INTERVAL 1 MONTH)returns the same astoStartOfMonth(t),toStartOfInterval(t, INTERVAL 1 DAY)returns the same astoStartOfDay(t),toStartOfInterval(t, INTERVAL 15 MINUTE)returns the same astoStartOfFifteenMinutes(t).
| Interval | Start |
|---|---|
| YEAR | year 0 |
| QUARTER | 1900 Q1 |
| MONTH | 1900 January |
| WEEK | 1970, 1st week (01-05) |
| DAY | 1970-01-01 |
| HOUR | (*) |
| MINUTE | 1970-01-01 00:00:00 |
| SECOND | 1970-01-01 00:00:00 |
| MILLISECOND | 1970-01-01 00:00:00 |
| MICROSECOND | 1970-01-01 00:00:00 |
| NANOSECOND | 1970-01-01 00:00:00 |
| (*) hour intervals are special: the calculation is always performed relative to 00:00:00 (midnight) of the current day. As a result, only | |
| hour values between 1 and 23 are useful. |
WEEK was specified, toStartOfInterval assumes that weeks start on Monday. Note that this behavior is different from that of function toStartOfWeek in which weeks start by default on Sunday.
The second overload emulates TimescaleDB’s time_bucket() function, respectively PostgreSQL’s date_bin() function.
Syntax
time_bucket, date_bin
Arguments
value— Date or date with time value to round down.DateorDateTimeorDateTime64x— Interval length number. -unit— Interval unit: YEAR, QUARTER, MONTH, WEEK, DAY, HOUR, MINUTE, SECOND, MILLISECOND, MICROSECOND, NANOSECOND. -time_zone— Optional. Time zone name as a string. -origin— Optional. Origin point for calculation (second overload only).
DateTime
Examples
Basic interval rounding
Query
Response
Query
Response
toStartOfMicrosecond
Introduced in: v22.6.0 Rounds down a date with time to the start of the microseconds. Syntaxdatetime— Date and time.DateTime64timezone— Optional. Timezone for the returned value. If not specified, the function uses the timezone of thevalueparameter.String
DateTime64
Examples
Query without timezone
Query
Response
Query
Response
toStartOfMillisecond
Introduced in: v22.6.0 Rounds down a date with time to the start of the milliseconds. Syntaxdatetime— Date and time.DateTime64timezone— Optional. Timezone for the returned value. If not specified, the function uses the timezone of thevalueparameter.String
DateTime64
Examples
Query without timezone
Query
Response
Query
Response
toStartOfMinute
Introduced in: v1.1.0 Rounds down a date with time to the start of the minute.The return type can be configured by setting
enable_extended_results_for_datetime_functions.datetime— A date with time to round.DateTimeorDateTime64
DateTime or DateTime64
Examples
Round down to the start of the minute
Query
Response
toStartOfMonth
Introduced in: v1.1.0 Rounds down a date or date with time to the first day of the month.The return type can be configured by setting
enable_extended_results_for_datetime_functions.value— The date or date with time to round down to the first day of the month.DateorDate32orDateTimeorDateTime64
Date
Examples
Round down to the first day of the month
Query
Response
toStartOfNanosecond
Introduced in: v22.6.0 Rounds down a date with time to the start of the nanoseconds. Syntaxdatetime— Date and time.DateTime64timezone— Optional. Timezone for the returned value. If not specified, the function uses the timezone of thevalueparameter.String
DateTime64
Examples
Query without timezone
Query
Response
Query
Response
toStartOfQuarter
Introduced in: v1.1.0 Rounds down a date or date with time to the first day of the quarter. The first day of the quarter is either 1 January, 1 April, 1 July, or 1 October.The return type can be configured by setting
enable_extended_results_for_datetime_functions.value— The date or date with time to round down to the first day of the quarter.DateorDate32orDateTimeorDateTime64
Date
Examples
Round down to the first day of the quarter
Query
Response
toStartOfSecond
Introduced in: v20.5.0 Rounds down a date with time to the start of the seconds. Syntaxdatetime— Date and time to truncate sub-seconds from.DateTime64timezone— Optional. Timezone for the returned value. If not specified, the function uses the timezone of thevalueparameter.String
DateTime64
Examples
Query without timezone
Query
Response
Query
Response
toStartOfTenMinutes
Introduced in: v20.1.0 Rounds down a date with time to the start of the nearest ten-minute interval.The return type can be configured by setting
enable_extended_results_for_datetime_functions.datetime— A date with time.DateTimeorDateTime64
DateTime or DateTime64
Examples
Example
Query
Response
toStartOfWeek
Introduced in: v20.1.0 Rounds a date or date with time down to the nearest Sunday or Monday.The return type can be configured by setting
enable_extended_results_for_datetime_functions.datetime— A date or date with time to convert.DateorDateTimeorDate32orDateTime64mode— Determines the first day of the week as described in thetoWeek()function. Default0.UInt8timezone— The timezone to use for the conversion. If not specified, the server’s timezone is used.String
Date or Date32
Examples
Round down to the nearest Sunday or Monday
Query
Response
toStartOfYear
Introduced in: v1.1.0 Rounds down a date or date with time to the first day of the year. Returns the date as aDate object.
The return type can be configured by setting
enable_extended_results_for_datetime_functions.value— The date or date with time to round down.DateorDate32orDateTimeorDateTime64
Date
Examples
Round down to the first day of the year
Query
Response
toTimeWithFixedDate
Introduced in: v1.1.0 Extracts the time component of a date or date with time. The returned result is an offset to a fixed point in time, currently1970-01-02,
but the exact point in time is an implementation detail which may change in future.
toTime should therefore not be used standalone.
The main purpose of the function is to calculate the time difference between two dates or dates with time, e.g., toTime(dt1) - toTime(dt2).
Syntax
date— Date to convert to a time.DateorDateTimeorDateTime64timezone— Optional. Timezone for the returned value.String
DateTime
Examples
Calculate the time difference between two dates
Query
Response
toTimezone
Introduced in: v1.1.0 Converts aDateTime or DateTime64 to the specified time zone.
The internal value (number of unix seconds) of the data doesn’t change.
Only the value’s time zone attribute and the value’s string representation changes.
Syntax
toTimeZone
Arguments
date— The value to convert.DateTimeorDateTime64timezone— The target time zone name.String
DateTime or DateTime64
Examples
Usage example
Query
Response
toUTCTimestamp
Introduced in: v23.8.0 Converts a date or date with time value from one time zone to UTC timezone timestamp. This function is mainly included for compatibility with Apache Spark and similar frameworks. Syntaxto_utc_timestamp
Arguments
datetime— A date or date with time type const value or an expression.DateTimeorDateTime64time_zone— A String type const value or an expression representing the time zone.String
DateTime or DateTime64
Examples
Convert timezone to UTC
Query
Response
toUnixTimestamp
Introduced in: v1.1.0 Converts aString, Date, or DateTime to a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC) as UInt32.
Syntax
date— Value to convert.DateorDate32orDateTimeorDateTime64orStringtimezone— Optional. Timezone to use for conversion. If not specified, the server’s timezone is used.String
UInt32
Examples
Usage example
Query
Response
toWeek
Introduced in: v20.1.0 This function returns the week number for date or datetime. The two-argument form oftoWeek() enables you to specify whether the week starts
on Sunday or Monday and whether the return value should be in the range from 0 to 53 or from 1 to 53.
toISOWeek() is a compatibility function that is equivalent to toWeek(date,3).
The following table describes how the mode argument works.
| Mode | First day of week | Range | Week 1 is the first week … |
|---|---|---|---|
| 0 | Sunday | 0-53 | with a Sunday in this year |
| 1 | Monday | 0-53 | with 4 or more days this year |
| 2 | Sunday | 1-53 | with a Sunday in this year |
| 3 | Monday | 1-53 | with 4 or more days this year |
| 4 | Sunday | 0-53 | with 4 or more days this year |
| 5 | Monday | 0-53 | with a Monday in this year |
| 6 | Sunday | 1-53 | with 4 or more days this year |
| 7 | Monday | 1-53 | with a Monday in this year |
| 8 | Sunday | 1-53 | contains January 1 |
| 9 | Monday | 1-53 | contains January 1 |
- If the week containing January 1 has 4 or more days in the new year, it is week 1.
- Otherwise, it is the last week of the previous year, and the next week is week 1.
String in a format supported by parseDateTime64BestEffort(). Support for string arguments exists only for reasons of compatibility with MySQL which is expected by certain 3rd party tools. As string argument support may in future be made dependent on new MySQL-compatibility settings and because string parsing is generally slow, it is recommended to not use it.
Syntax
week
Arguments
datetime— Date or date with time to get the week number from.DateorDateTimemode— Optional. A mode0to9determines the first day of the week and the range of the week number. Default0. -time_zone— Optional. Time zone.String
UInt32
Examples
Get week numbers with different modes
Query
Response
toYYYYMM
Introduced in: v1.1.0 Converts a date or date with time to aUInt32 number containing the year and month number (YYYY * 100 + MM).
Accepts a second optional timezone argument. If provided, the timezone must be a string constant.
This function is the opposite of function YYYYMMDDToDate().
Syntax
datetime— A date or date with time to convert.DateorDate32orDateTimeorDateTime64timezone— Optional. Timezone for the conversion. If provided, the timezone must be a string constant.String
UInt32
Examples
Convert current date to YYYYMM format
Query
Response
toYYYYMMDD
Introduced in: v1.1.0 Converts a date or date with time to aUInt32 number containing the year and month number (YYYY * 10000 + MM * 100 + DD). Accepts a second optional timezone argument. If provided, the timezone must be a string constant.
Syntax
datetime— A date or date with time to convert.DateorDate32orDateTimeorDateTime64timezone— Optional. Timezone for the conversion. If provided, the timezone must be a string constant.String
UInt32 number containing the year, month and day (YYYY * 10000 + MM * 100 + DD). UInt32
Examples
Convert current date to YYYYMMDD format
Query
Response
toYYYYMMDDhhmmss
Introduced in: v1.1.0 Converts a date or date with time to aUInt64 number containing the year and month number (YYYY * 10000000000 + MM * 100000000 + DD * 1000000 + hh * 10000 + mm * 100 + ss).
Accepts a second optional timezone argument. If provided, the timezone must be a string constant.
Syntax
datetime— Date or date with time to convert.DateorDate32orDateTimeorDateTime64timezone— Optional. Timezone for the conversion. If provided, the timezone must be a string constant.String
UInt64 number containing the year, month, day, hour, minute and second (YYYY * 10000000000 + MM * 100000000 + DD * 1000000 + hh * 10000 + mm * 100 + ss). UInt64
Examples
Convert current date and time to YYYYMMDDhhmmss format
Query
Response
toYear
Introduced in: v1.1.0 Returns the year component (AD) of aDate or DateTime value.
Syntax
YEAR
Arguments
datetime— Date or date with time to get the year from.DateorDate32orDateTimeorDateTime64
UInt16
Examples
Usage example
Query
Response
toYearNumSinceEpoch
Introduced in: v25.3.0 Returns amount of years passed from year 1970 Syntaxdate— A date or date with time to convert.DateorDateTimeorDateTime64
Query
Response
toYearWeek
Introduced in: v20.1.0 Returns the year and week for a date. The year in the result may be different from the year in the date argument for the first and the last week of the year. The mode argument works like the mode argument oftoWeek().
Warning: The week number returned by toYearWeek() can be different from what the toWeek() returns. toWeek() always returns week number in the context of the given year, and in case toWeek() returns 0, toYearWeek() returns the value corresponding to the last week of previous year. See prev_yearWeek in example below.
The first argument can also be specified as String in a format supported by parseDateTime64BestEffort(). Support for string arguments exists only for reasons of compatibility with MySQL which is expected by certain 3rd party tools. As string argument support may in future be made dependent on new MySQL-compatibility settings and because string parsing is generally slow, it is recommended to not use it.
Syntax
yearweek
Arguments
datetime— Date or date with time to get the year and week of.DateorDateTimemode— Optional. A mode0to9determines the first day of the week and the range of the week number. Default0. -timezone— Optional. Time zone.String
UInt32
Examples
Get year-week combinations with different modes
Query
Response
today
Introduced in: v1.1.0 Returns the current date at moment of query analysis. Same astoDate(now()).
Syntax
curdate, current_date
Arguments
- None.
Date
Examples
Usage example
Query
Response
Query
Response
yesterday
Introduced in: v1.1.0 Accepts zero arguments and returns yesterday’s date at one of the moments of query analysis. Syntax- None.
Date
Examples
Get yesterday’s date
Query
Response