- Create date and time values.
- Parse and format date and time strings
- Extract specific components (year, month, day, hour, etc.)
- Convert between time zones
- Perform date and time calculations
Working with time zones
Most functions accept an optional time zone argument (e.g., ‘Europe/Amsterdam’ or ‘US/Pacific’). This example shows how a timestamp is interpreted in different time zones:order_timeis the original timestamplocal_dateis the date in the local time zonemexico_city_dateis the date in Mexico Cityhawaii_timeis the full timestamp in Hawaii
ClickHouse function reference
makeDate
Creates aDate from:
- a year, month and day argument, or
- a year and day of year argument.
- MAKEDATE(year, month, day)
- MAKEDATE(year, day_of_year)
year(numeric): Year.month(numeric): Month.day(numeric): Day.day_of_year(numeric): Day of the year.
- A date created from the arguments. [
Date].
year(numeric): Year.month(numeric): Month.day(numeric): Day.day_of_year(numeric): Day of the year.
- A date created from the arguments. [
Date32].
makeDate, but returns a Date32 type which supports a wider range of dates (from year 1900 to 2299).
makeDateTime
Creates aDateTime from year, month, day, hour, minute and second arguments.
Syntax:
year(numeric): Year.month(numeric): Month.day(numeric): Day.hour(numeric): Hour.minute(numeric): Minute.second(numeric): Second.timezone(String, optional): Timezone for the returned value.
- A date with time created from the arguments.
DateTime.
DateTime representing the moment when a special taco promotion ends.
makeDateTime64
Creates aDateTime64 value from year, month, day, hour, minute, second and optional fractional seconds components.
Syntax:
year(numeric): Year.month(numeric): Month (1-12).day(numeric): Day (1-31).hour(numeric): Hour (0-23).minute(numeric): Minute (0-59).second(numeric): Second (0-59).fraction(numeric, optional): Fractional seconds.precision(UInt8, optional): Precision of the fractional seconds (0-9).timezone(String, optional): Timezone name.
DateTime64 value created from the supplied arguments.
Example:
DateTime64 value for May 15, 2024 at 10:30:45.779 AM with millisecond precision.
Syntax:
- TIMESTAMP
expr(String) — Date or date with time.expr_time(String, optional) — Time to add.
- A
DateTime64(6)value.
- The first query converts a date string to a DateTime64 value.
- The second query adds 12 hours and 0.11 seconds to the initial date and time.
timeZone
Returns the timezone of the current session. Syntax:- timezone
- The timezone of the current session as a
String.
session_timezone setting. If executed in the context of a distributed table, it generates a column with values relevant to each shard. Otherwise, it produces a constant value.
See Also:
serverTimeZone()- Returns the timezone of the server.
serverTimeZone
Returns the timezone of the server, i.e. the value of settingtimezone. 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
- Timezone name. (
String)
timeZone()function for getting the current session’s timezone
Propel always uses UTC.
toTimeZone
Converts a date or date with time to the specified time zone. The function changes the timezone attribute of the value and adjusts the value’s string representation accordingly, but does not modify the underlying timestamp. Syntaxvalue(DateTime64): Date and time.timezone(String): Timezone for the returned value.
- Date and time in the specified timezone. (
DateTime)
- We start with a UTC timestamp for January 1, 2019, at midnight.
- We convert it to the Los Angeles timezone, which is 8 hours behind UTC.
- Note that the integer representation (seconds since epoch) remains the same, but the string representation changes to reflect the new timezone.
The
toTimeZone function only changes the timezone attribute and the string representation. It does not perform any time arithmetic. If you need to adjust the actual time value, consider using functions like addHours or subtractHours in combination with toTimeZone.timeZoneOf
Returns the timezone name of a DateTime or DateTime64 value. Syntax:value(DateTimeorDateTime64): Date and time.
- Timezone name.
String.
timeZoneOf(). This could be useful when working with taco delivery timestamps from different regions.
timeZoneOffset
Returns the timezone offset in seconds from UTC for the specified date and time. Syntax:value(DateTimeorDateTime64): Date and time.
- Offset from UTC in seconds.
Int32.
- We create a DateTime value for April 21, 2023, at 10:20:30 AM in the ‘America/Los_Angeles’ timezone.
- The
timeZoneOffsetfunction returns -25200 seconds, which is equivalent to -7 hours. - This indicates that Los Angeles was 7 hours behind UTC at that specific date and time.
toYear
Extracts the year from a date or date with time value. Syntax- YEAR
date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The year number (AD).
- Type:
UInt16
toYear extracts the year (2023) from the given date and time.
This function is particularly useful for grouping data by year or filtering records based on specific years.
toQuarter
Returns the quarter (1-4) of a date or date with time. Syntax:- QUARTER
value(Date,Date32,DateTime, orDateTime64): A date or date with time.
- The quarter of the year (1, 2, 3 or 4) of the given date/time.
UInt8.
This function is useful for grouping data by quarters or for time-based analysis where quarterly information is needed.
toMonth
Returns the month component (1-12) of a date or date with time. Syntax:- MONTH
value(Date,Date32,DateTime, orDateTime64): A date or date with time.
- The month of the year (1-12) of the given date/time. Type:
UInt8.
For dates outside the supported range, the behavior is implementation-specific. ClickHouse may return zero, throw an exception, or perform a “natural” overflow.
toDayOfYear
Returns the number of the day within the year (1-366) for a given date or date with time. Syntax- DAYOFYEAR
date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The day of the year (1-366) for the given date/time.
- Type:
UInt16
toDayOfMonth
Returns the number of the day within the month (1-31) for a given date or date with time. Syntax- DAYOFMONTH
- DAY
value(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The day of the month (1-31) of the given date/time.
UInt8.
toDayOfMonth extracts the day (15) from the given date.
toDayOfWeek
Returns the number of the day within the week for a date or date with time. Syntaxt(Date,Date32,DateTime, orDateTime64): Date or date with time.mode(UInt8, optional): Determines the first day of the week and return value range. Default is 0.timezone(String, optional): Timezone for the returned value.
| Mode | First day of week | Range |
|---|---|---|
| 0 | Monday | 1-7 (Monday = 1, …, Sunday = 7) |
| ותו | Monday | 0-6 (Monday = 0, …, Sunday = 6) |
| 2 | Sunday | 0-6 (Sunday = 0, Monday = 1, …, Saturday = 6) |
| 3 | Sunday | 1-7 (Sunday = 1, Monday = 2, …, Saturday = 7) |
- The day of the week as an integer, depending on the chosen mode.
If the first argument is a string, it will be parsed as a date using the format supported by
parseDateTime64BestEffort(). However, using string arguments is not recommended for performance reasons.toHour
Returns the hour component (0-23) of a date with time.Assumes that if clocks are moved ahead, it is by one hour and occurs at 2 a.m., and if clocks are moved back, it is by one hour and occurs at 3 a.m. (which is not always exactly when it occurs - it depends on the timezone).
- HOUR
value(DateTimeorDateTime64): A date/time value.
- The hour of the day (0 - 23) of the given date/time. [
UInt8]
toMinute
Returns the minute component (0-59) of a date with time. Syntax:- MINUTE
value(DateTimeorDateTime64): A DateTime or DateTime64 value.
- The minute of the hour (0 - 59) of the given date/time.
- Type:
UInt8.
toSecond
Returns the second component (0-59) of a date with time. Leap seconds are not considered. Syntax:- SECOND
value(DateTimeorDateTime64): A DateTime or DateTime64 value.
- The second in the minute (0 - 59) of the given date/time. [
UInt8]
toMillisecond
Returns the millisecond component (0-999) of a date with time. Syntaxvalue(DateTimeorDateTime64): The input date and time value.
- The millisecond in the second (0-999) of the given date/time.
- Type:
UInt16
If the input is a DateTime (which doesn’t store milliseconds), the function will always return 0.
toUnixTimestamp
Converts a date, date with time, or string-encoded date/time to a Unix timestamp. Syntaxdate(Date,DateTime, orDateTime64): Date or date with time.str(String): String representing a date or date with time.timezone(String, optional): Timezone for the returned value.
- The Unix timestamp. (
UInt32).
unix_timestampis calculated using the server’s timezone.unix_timestamp_lais calculated using the ‘America/Los_Angeles’ timezone.
When using the string format, the function accepts an optional timezone argument. This allows you to specify the timezone of the input string if it’s different from the server’s timezone.
toStartOfYear
Rounds down a date or date with time to the first day of the year. Syntaxvalue(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The first day of the year for the given date/time.
Date.
toStartOfISOYear
Rounds down a date or date with time to the first day of the ISO year. Syntaxvalue(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The first day of the ISO year for the given date/time.
Date.
The ISO year can differ from the calendar year. For dates close to the new year,
toStartOfISOYear may return a date from the previous or next calendar year.toStartOfQuarter
Rounds down a date or date with time to the first day of the quarter. The first day of the quarter is either January 1, April 1, July 1, or October 1. Syntaxvalue(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The first day of the quarter for the given date/time.
Date.
toStartOfMonth
Rounds down a date or date with time to the first day of the month. Syntaxvalue(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The first day of the month for the given date/time.
Date.
The behavior of parsing incorrect dates is implementation specific. ClickHouse may return zero date, throw an exception, or do “natural” overflow.
toLastDayOfMonth
Rounds a date or date with time to the last day of the month. Returns the date. Syntax- LAST_DAY
value(Date,Date32,DateTime, orDateTime64): A date or date with time.
- The last day of the month for the given date/time.
Date.
toLastDayOfMonth. The function returns the last day of April, which is the 30th.
toMonday
Rounds down a date or date with time to the nearest Monday. Returns the date. Syntaxvalue(Date,Date32,DateTime, orDateTime64): A date or date with time.
- The date of the nearest Monday on or prior to the given date.
Date.
- For the Friday date ‘2023-04-21’,
toMondayreturns the previous Monday ‘2023-04-17’. - For the Monday date ‘2023-04-24’,
toMondayreturns the same date since it’s already a Monday.
t(Date,Date32,DateTime, orDateTime64) — Date or date with time.mode(UInt8, optional) — Determines the first day of the week:0(default): Sunday1: Monday
timezone(String, optional) — Timezone parameter.
- The date of the nearest Sunday or Monday on or prior to the given date, depending on the mode. (
Date).
default_modeanddate_defaultround down to the previous Sundaymonday_modeanddate_mondayround down to the previous Monday (or stay on Monday if it’s already a Monday)
toLastDayOfWeek
Rounds a date or date with time up to the nearest Saturday or Sunday. The mode argument works exactly like the mode argument in function toWeek(). If no mode is specified, mode is assumed as 0. Syntaxt(Date,Date32,DateTime, orDateTime64): A date or date with time.mode(UInt8, optional): Determines the last day of the week as described in the toWeek function.timezone(String, optional): It behaves like any other conversion function.
- The date of the nearest Sunday or Monday on or after the given date, depending on the mode. (
Date).
- For mode 0 (default), the week ends on Saturday.
- For mode 1, the week ends on Sunday.
- When the input date is already the last day of the week for the given mode, it returns that date.
toStartOfDay
Rounds down a date or date with time to the start of the day. Syntaxvalue(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The start of the day for the given date/time.
DateTime.
toStartOfDay rounds down the given date and time to the start of the day (midnight).
toStartOfHour
Rounds down a date with time to the start of the hour. Syntax:value(DateTimeorDateTime64): A date with time.
- The start of the hour for the given date and time.
- Type:
DateTime.
rounded_hourshows the start of the hour for a regular DateTime.rounded_hour_precisedemonstrates that the function also works with DateTime64, preserving the precision but setting the minutes, seconds, and sub-seconds to zero.
toStartOfMinute
Rounds down a date with time to the start of the minute. Syntaxvalue(DateTimeorDateTime64): A date with time.
- The start of the minute for the given date and time.
- Type:
DateTime
start_of_minuteshows the result for a DateTime inputstart_of_minute_64shows the result for a DateTime64 input with precision of 4 decimal places
toStartOfSecond
Truncates sub-seconds from a DateTime64 value. Syntaxvalue(DateTime64): Date and time.timezone(String, optional): Timezone for the returned value.
- Input value without sub-seconds. (
DateTime64).
toStartOfMillisecond
Truncates a DateTime64 value to the start of the millisecond. Syntaxvalue(DateTime64): Date and time.timezone(String, optional): Timezone for the returned value.
- Input value truncated to the start of the millisecond. (
DateTime64).
toStartOfMicrosecond
Truncates a DateTime64 value to the start of the microsecond. Syntaxvalue(DateTime64): Date and time.timezone(String, optional): Timezone for the returned value.
- Input value truncated to the start of the microsecond. (
DateTime64).
value(DateTime64): Date and time.timezone(String, optional): Timezone for the returned value.
- Input value truncated to the start of the nanosecond.
DateTime64.
In this example, the function doesn’t change the input value because it’s already at the start of a nanosecond.
Note that the time is adjusted for the Moscow timezone, which is 3 hours ahead of UTC.
toStartOfFiveMinutes
Rounds down a date with time to the start of the five-minute interval. Syntax:value(DateTimeorDateTime64): A date with time.
- The start of the five-minute interval of the given date/time.
- Type:
DateTime.
toStartOfTenMinutes
Rounds down a date with time to the start of the ten-minute interval. Syntaxvalue(DateTimeorDateTime64): A date with time.
- The start of the ten-minute interval for the given date and time.
- Type:
DateTime.
10:17:00is rounded down to10:10:0010:20:00remains10:20:00as it’s already at the start of a ten-minute interval10:23:00is rounded down to10:20:00
toStartOfFifteenMinutes
Rounds down a date with time to the start of the fifteen-minute interval. Syntax:value(DateTimeorDateTime64): A date with time.
- The start of the fifteen-minute interval of the given date/time.
- Type:
DateTime.
10:17:00is rounded down to10:15:0010:30:00remains10:30:00as it’s already at the start of a 15-minute interval10:45:59is rounded down to10:45:00
toStartOfInterval
Rounds down a date or date with time to the start of the specified interval. Syntaxtime_or_data(Date,DateTime, orDateTime64): Date, DateTime, or DateTime64 to round down.x(UInt8): Number of interval units.unit(String): The type of interval. Possible values:year,quarter,month,week,dayhour,minute,secondmillisecond,microsecond,nanosecond
time_zone(String, optional): Optional timezone name (e.g. ‘America/New_York’).
- Rounded down date or date with time, matching the type of the input.
toStartOf*() functions. For example:
toStartOfInterval(t, INTERVAL 1 YEAR)is equivalent totoStartOfYear(t)toStartOfInterval(t, INTERVAL 1 MONTH)is equivalent totoStartOfMonth(t)toStartOfInterval(t, INTERVAL 1 DAY)is equivalent totoStartOfDay(t)toStartOfInterval(t, INTERVAL 15 MINUTE)is equivalent totoStartOfFifteenMinutes(t)
- For
WEEKintervals, weeks are considered to start on Monday. - Hour intervals are special: calculations are always performed relative to 00:00:00 (midnight) of the current day.
toTime
Converts a date with time to a certain fixed date, while preserving the time. Syntax:date(Date/DateTime/DateTime64): Date to convert to a time.timezone(String, optional): Timezone for the returned value.
DateTimewith date equated to 1970-01-02 while preserving the time.
If the date input argument contained sub-second components, they will be dropped in the returned DateTime value with second-accuracy.
- The input
2023-09-15 12:30:45.6789is converted to1970-01-02 12:30:45. - The date is changed to the fixed date
1970-01-02. - The time
12:30:45is preserved. - Sub-second precision (
.6789) is dropped as the result is aDateTimetype.
toRelativeYearNum
Converts a date or date with time to the number of years elapsed since a certain fixed point in the past. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The number of years from a fixed reference point in the past.
UInt16.
toRelativeYearNum works with both Date and DateTime inputs, returning the year number relative to the internal reference point.
The fixed reference point is implementation-defined and may change in future versions. This function is primarily used for internal operations and comparisons rather than for displaying actual year values.
toRelativeQuarterNum
Converts a date or date with time to the number of quarters elapsed since a certain fixed point in the past. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The number of quarters from a fixed reference point in the past.
UInt32.
toRelativeMonthNum
Converts a date or date with time to the number of months elapsed since a certain fixed point in the past. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The number of months from a fixed reference point in the past.
UInt32.
m1shows the number of months from the reference point to April 25, 2001.m2shows the number of months from the reference point to July 8, 2009.
m2 and m1 (99) represents the number of months between these two dates.
toRelativeWeekNum
Converts a date or date with time to the number of weeks elapsed since a certain fixed point in the past. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The number of weeks from a fixed reference point in the past.
UInt32.
w1shows the number of weeks from the fixed point to February 29, 2000.w2shows the number of weeks from the fixed point to January 12, 2001.
toRelativeDayNum
Converts a date or date with time to the number of days elapsed since a certain fixed point in the past. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The number of days from a fixed reference point in the past.
UInt32.
toRelativeHourNum
Converts a date or date with time to the number of hours elapsed since a certain fixed point in the past. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The number of hours from a fixed reference point in the past.
UInt32.
taco_order_hourshows the number of hours elapsed since the reference point to the taco order time.taco_delivery_hourshows the number of hours elapsed to the taco delivery time, which is 6 hours later.
date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The number of minutes from a fixed reference point in the past.
UInt32.
toRelativeSecondNum
Converts a date or date with time to the number of seconds elapsed since a certain fixed point in the past. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The number of seconds from a fixed reference point in the past.
UInt32.
- We convert two timestamps to their relative second numbers.
- The difference between the two values is 30 seconds (1683290975 - 1683290945 = 30).
toISOYear
Converts a date or date with time to the ISO Year number. Syntaxdate(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The ISO Year number.
Type:
UInt16
The ISO year can differ from the calendar year for dates close to year boundaries. The ISO year starts with the Monday of the week containing January 4th.
toISOWeek
Converts a date or date with time to aUInt8 number containing the ISO Week number.
Syntax:
date(Date,Date32,DateTime, orDateTime64): Date or date with time.
- The ISO week number (1-53) of the given date/time.
UInt8.
- April 21, 2023 is in the 16th ISO week of the year.
- December 31, 2023 is in the 52nd ISO week of the year.
The ISO week number may differ from the calendar week number. The first ISO week of a year is the week that contains the first Thursday of that year.
toWeek
Returns the week number for a date or datetime. The two-argument form allows 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. If the mode argument is omitted, the default mode is 0. 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.
- WEEK
t(DateorDateTime): Date or DateTime.mode(UInt8, optional): Optional parameter, range of values is [0,9], default is 0.timezone(String, optional): Optional parameter, it behaves like any other conversion function.
- The week number as a
UInt8value.
week0(mode 0) returns 21 because it’s the 21st Sunday-starting week of 2024.week1(mode 1) returns 22 because it’s the 22nd Monday-starting week of 2024.week9(mode 9) returns 22 because it’s the 22nd Monday-starting week of 2024 that contains January 1.
toYearWeek
Returns the year and week number for a given 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 function has two modes of operation:- Single-argument syntax (uses mode value of 0)
- Two-argument syntax (allows specifying a mode)
date(Date,DateTime, orDateTime64): Date or date with time.mode(UInt8, optional): Determines how the week is calculated.
mode argument works like the mode argument in toWeek(). It affects which day is considered the first day of the week and whether the return value should be in the range 0-53 or 1-53.
Returns
UInt32: A number containing the year and week number (YYYYWW).
The week number returned by
toYearWeek() can be different from what toWeek() returns. toWeek() always returns the week number in the context of the given year, while toYearWeek() may return a week number corresponding to the next year for dates at the end of December.toDaysSinceYearZero
Returns the number of days passed since January 1, 0000 in the proleptic Gregorian calendar for a given date. Syntax- TO_DAYS
date(Date,Date32,DateTime, orDateTime64): The date to calculate the number of days from.time_zone(String, optional): Optional timezone parameter.
- The number of days passed since date 0000-01-01. (
UInt32).
This function uses the same calculation method as MySQL’s
TO_DAYS() function.- fromDaysSinceYearZero — The inverse operation, converting days to a date.
fromDaysSinceYearZero
Returns a date corresponding to the number of days passed since January 1, 0000 in the proleptic Gregorian calendar. Syntax- FROM_DAYS
days(UInt32): The number of days passed since year zero.
- The date corresponding to the number of days passed since year zero. (
Date)
- We convert 739136 days (which corresponds to September 8, 2023) back to a date.
- We also demonstrate round-trip conversion by first converting ‘2023-09-08’ to days since year zero, then back to a date.
The result is undefined if it cannot be represented within the bounds of the Date type.
fromDaysSinceYearZero
Returns a date corresponding to the number of days passed since January 1, 0000 in the proleptic Gregorian calendar. Syntax- FROM_DAYS
days(UInt32): The number of days passed since year zero.
- The date corresponding to the number of days passed since year zero. (
Date)
- We convert 739136 days (which corresponds to September 8, 2023) back to a date.
- We also demonstrate round-trip conversion by first converting ‘2023-09-08’ to days since year zero, then back to a date.
The result is undefined if it cannot be represented within the bounds of the Date type.
age
Calculates the difference between two dates or timestamps in the specified unit. Syntax:unit(String): The unit to measure the difference in. Possible values:- ‘nanosecond’, ‘microsecond’, ‘millisecond’
- ‘second’, ‘minute’, ‘hour’
- ‘day’, ‘week’, ‘month’, ‘quarter’, ‘year’
startdate(Date,DateTime, orDateTime64): The earlier date/time.enddate(Date,DateTime, orDateTime64): The later date/time.timezone(optionalString): The timezone to use for the calculation.
enddate and startdate expressed in the specified unit. [Int]
Example:
- The difference is 3 days
- 0 months (since it’s within the same month)
- 0 years (since it’s within the same year)
The
age function calculates the difference using a precision of 1 nanosecond. For an alternative that counts unit boundaries crossed, see the date_diff function.date_diff
Calculates the difference between two dates or timestamps in the specified unit. Syntax:- dateDiff
- DATE_DIFF
- timestampDiff
- timestamp_diff
- TIMESTAMP_DIFF
unit(String): The unit of time for the result. Possible values:- ‘nanosecond’, ‘microsecond’, ‘millisecond’
- ‘second’, ‘minute’, ‘hour’
- ‘day’, ‘week’, ‘month’, ‘quarter’, ‘year’
startdate(DateorDateTimeorDateTime64): The earlier date or timestamp.enddate(DateorDateTimeorDateTime64): The later date or timestamp.timezone(String, optional): Optional timezone name for the calculation.
startdate and enddate. (Int)
Example:
days_diffcalculates the number of days between May 1st and May 15th, 2023.months_diffcalculates the number of complete months between the start and end of 2023.
- The function uses relative units. For example, the difference between 2023-12-29 and 2024-01-01 is 3 days, 1 month, and 1 year.
- For the ‘week’ unit, weeks are assumed to start on Monday.
date_trunc
Truncates a date or timestamp to the specified unit. Syntaxunit(String): The unit to truncate to. [String Literal] Possible values (case-insensitive):- ‘second’
- ‘minute’
- ‘hour’
- ‘day’
- ‘week’
- ‘month’
- ‘quarter’
- ‘year’
value(Date/DateTime/DateTime64): Date or timestamp to truncate.timezone(String, optional): Timezone for the returned value.
- Truncated date or timestamp. [
DateTime]
truncated_monthshows the date truncated to the start of the month.truncated_day_lashows the date truncated to the start of the day in the Los Angeles timezone.
The
date_trunc function is useful for grouping time series data by specific time intervals.date_add
Adds a time interval to a date or date with time. Syntax:- dateAdd
- DATE_ADD
unit— The type of interval to add. Possible values:- second
- minute
- hour
- day
- week
- month
- quarter
- year
value(Int) — Value of interval to add.date(Date,Date32,DateTime, orDateTime64) — The date or date with time to which value is added.
value, expressed in unit, to date. Returns the same type as the input date.
Example:
The
unit argument is not a string and should not be quoted.date_sub
Subtracts a time interval from a date or date with time. Syntax:unit(String) — The type of interval to subtract. Possible values:- second
- minute
- hour
- day
- week
- month
- quarter
- year
value(Int) — Number of interval units to subtract.date(Date,Date32,DateTime, orDateTime64) — The date or date with time to subtract from.
- Date or date with time obtained by subtracting the specified interval. Same type as the input
date.
If the subtraction results in a date outside the valid range for the data type, the result is undefined.
timestamp_add
Adds the specified time interval to a date or date with time value. Syntax:- timeStampAdd
- TIMESTAMP_ADD
date(Date,Date32,DateTime, orDateTime64): Date or date with time.value(Int): Value of interval to add.unit(String): The type of interval to add. Possible values:- second
- minute
- hour
- day
- week
- month
- quarter
- year
date.
Example:
If the addition results in a value outside the bounds of the data type, the result is undefined.
timestamp_sub
Subtracts the time interval from the provided date or date with time. If the subtraction results in a value outside the bounds of the data type, the result is undefined. Syntax:- timeStampSub
- TIMESTAMP_SUB
unit(String): The type of interval to subtract. Possible values:- second
- minute
- hour
- day
- week
- month
- quarter
- year
value(Int): Value of interval to subtract.date(Date,Date32,DateTime, orDateTime64): Date or date with time.
value, expressed in unit, from date. (Date, Date32, DateTime, or DateTime64).
Example:
addDate
Adds a time interval to a date, date with time, or string-encoded date/time value. Syntax:date(Date,Date32,DateTime,DateTime64, orString): The date or date with time to which the interval is added.interval(Interval): The interval to add.
- Date or date with time obtained by adding the interval to the input date. The return type matches the input type.
If the addition results in a value outside the bounds of the data type, the result is undefined.
- date_add — An alternative function for adding intervals to dates.
subDate
Subtracts the time interval from the provided date, date with time or String-encoded date / date with time.If the subtraction results in a value outside the bounds of the data type, the result is undefined.
- SUBDATE
date(Date,Date32,DateTime,DateTime64, orString): The date or date with time from whichintervalis subtracted.interval(Interval): Interval to subtract.
interval from date. (Date, Date32, DateTime or DateTime64).
Example:
now
Returns the current date and time at the moment of query analysis. The function is a constant expression. Syntax:- current_timestamp
timezone(String, optional) — Timezone name for the returned value.
- Current date and time. (
DateTime)
For scenarios where you need the current time for each row in long-running INSERT SELECT queries, consider using the
nowInBlock() function instead.now64
Returns the current date and time with sub-second precision at the moment of query analysis. The function is a constant expression. Syntax:scale(UInt8, optional): Tick size (precision): 10^-precision seconds. Valid range: [0:9]. Typically used values:- 3 (default) - milliseconds
- 6 - microseconds
- 9 - nanoseconds
timezone(String, optional): Optional timezone for the returned value.
DateTime64]
Example:
now64()returns the current time with millisecond precision in the server’s timezone.now64(9, 'America/Los_Angeles')returns the current time with nanosecond precision in the Los Angeles timezone.
The actual values will differ based on when and where you run the query.
nowInBlock
Returns the current date and time at the moment of processing each block of data. Unlike thenow() function, it is not a constant expression, and the returned value will be different in different blocks for long-running queries.
This function is useful for generating the current time in long-running INSERT SELECT queries.
Syntax
timezone(String, optional): Timezone name for the returned value.
- Current date and time at the moment of processing each block of data. (
DateTime)
static_time remains constant as it’s evaluated once at query analysis, while block_time updates for each block, simulating a scenario where you might be processing taco orders in real-time across different time blocks.
The
sleep(1) function is used to introduce a delay between blocks, making the time difference more noticeable. In real-world scenarios, this delay would be replaced by actual data processing.today
Returns the current date at the moment of query analysis. Syntax:- curdate()
- current_date()
- The current date. (
Date)
toDate(now()).
The result is calculated at query analysis time, not at execution time. This means the function returns the same value for all rows in a query, even if the query takes a long time to execute.
yesterday
Returns the date of the previous day. Syntax:Date value representing yesterday’s date.
Example:
yesterday() returns the date immediately before today(), which is equivalent to subtracting 1 day from today’s date.
The
yesterday() function is a constant expression, meaning it returns the same value for all rows in the query and is evaluated once at query analysis time.timeSlot
Rounds the time to the half hour. Syntax:time(DateTimeorDateTime64): Date with time.
- DateTime rounded down to the nearest half hour.
- Type:
DateTime.
- ‘12:17:00’ is rounded down to ‘12:00:00’
- ‘12:45:00’ is rounded down to ‘12:30:00’
toYYYYMM
Converts a date or date with time to aUInt32 number containing the year and month number (YYYY * 100 + MM).
Syntax:
date(Date,Date32,DateTime, orDateTime64): Date or date with time.timezone(String, optional): Timezone name. If provided, it must be a string constant.
- A
UInt32number representing the year and month (YYYYMM).
This function is the opposite of the
YYYYMMDDToDate() function.toYYYYMMDD
Converts a date or date with time to aUInt32 number containing the year, month, and day (YYYY * 10000 + MM * 100 + DD).
Syntax
date(Date,Date32,DateTime, orDateTime64): Date or date with time.timezone(String, optional): Timezone name. If provided, it must be a string constant.
- A
UInt32number representing the date in YYYYMMDD format.
This function is the opposite of the YYYYMMDDToDate function.
toYYYYMMDDhhmmss
Converts a date or date with time to aUInt64 number containing the year, month, day, hour, minute and second components.
Syntax
date(Date,DateTime, orDateTime64): Date or date with time.timezone(String, optional): Timezone for the returned value.
- A
UInt64number in the formatYYYYMMDDhhmmss.
This function is useful for generating compact datetime representations for sorting or unique identifiers. It’s particularly handy when you need to order your taco delivery timestamps!
YYYYMMDDToDate
Converts a number containing the year, month and day to a Date. Syntax:yyyymmdd(Integer,Float, orDecimal): A number representing the year, month and day.
- A date created from the arguments. (
Date).
toYYYYMMDD() function.
The output is undefined if the input does not encode a valid Date value.
YYYYMMDDToDate32
Converts a number containing the year, month and day to aDate32 value.
Syntax
yyyymmdd(numeric): A number representing the year, month and day.Integer,FloatorDecimal.
- A date created from the arguments.
Date32.
20230911 (representing September 11, 2023) to a Date32 value. This could be useful for converting numeric date representations into actual date values, perhaps for a taco shop’s event planning or sales analysis.
The output is undefined if the input does not encode a valid Date32 value. Always ensure your input represents a valid date to avoid unexpected results.
YYYYMMDDhhmmssToDateTime
Converts a string containing a date and time in the format ‘YYYYMMDDHHMMSS’ to a DateTime value. Syntaxdatetime_string(String): A string in the format ‘YYYYMMDDHHMMSS’.timezone(String, optional): Optional parameter specifying the timezone for the returned value.
- A DateTime value created from the input string. (
DateTime)
If the input string does not represent a valid date and time, the function will return an undefined result. Always ensure your input is in the correct format to avoid unexpected behavior.
YYYYMMDDhhmmssToDateTime64
Converts a string containing a date and time in the format ‘YYYYMMDDHHMMSS’ to a DateTime64 value. Syntaxdatetime_string(String): A string in the format ‘YYYYMMDDHHMMSS’.precision(UInt8, optional): The precision of the resulting DateTime64. Default is 3 (milliseconds).timezone(String, optional): The timezone to use for the conversion.
- A DateTime64 value representing the input date and time. (
DateTime64)
If the input string is not in the correct format or represents an invalid date/time, the function will throw an exception. Always validate your input data to ensure it’s in the correct format before using this function.
changeYear
Changes the year component of a date or date time. Syntax:date_or_datetime(Date,Date32,DateTime, orDateTime64): A date or date time value.value(Int32): A new value of the year.
date_or_datetime.
Example:
- The year of the
Datevalue is changed from 1999 to 2000. - The year of the
DateTime64value is changed from 1999 to 2000, preserving the time component.
The function preserves the month and day components of the original date, adjusting only the year. If the original date was February 29th of a leap year and the new year is not a leap year, the result will be February 28th of the new year.
changeMonth
Changes the month component of a date or date time. Syntax:date_or_datetime(Date,Date32,DateTime, orDateTime64): A date or date time value.value(Int8,Int16,Int32, orInt64): A new value of the month.
date_or_datetime.
Example:
new_datechanges the month of the date to February (2).new_datetimechanges the month of the date time to February (2) while preserving the time component.
changeDay
Changes the day component of a date or date time. Syntax:date_or_datetime(Date,Date32,DateTime, orDateTime64): A date or date with time.value(Integer): New value for the day.
- Returns a value of the same type as
date_or_datetime.
new_datechanges the day of ‘2023-05-05’ to the 15th.new_datetimechanges the day of ‘2023-05-05 12:30:45.123’ to the 15th while preserving the time.
changeHour
Changes the hour component of a date or date time. Syntax:date_or_datetime(Date,Date32,DateTime, orDateTime64): A date or date with time.value(Integer): New value for the hour.
- Returns a value of the same type as
date_or_datetime. - If the input is a
Date, it returns aDateTime. - If the input is a
Date32, it returns aDateTime64.
date_to_datetimechanges the hour of a Date to 14 and converts it to a DateTime.change_hour_datetime64changes the hour of a DateTime64 to 14 while preserving minutes, seconds, and milliseconds.
Note that when changing the hour of a Date, it’s automatically converted to a DateTime with the time set to the specified hour and 00:00 for minutes and seconds.
changeMinute
Changes the minute component of a date or date with time. Syntax:date_or_datetime(Date,Date32,DateTime, orDateTime64): A date or date with time.value(Integer): New value for the minute component.
date_or_datetime. If the input is a Date, it returns a DateTime. If the input is a Date32, it returns a DateTime64.
Example:
date_changeconverts the Date to DateTime and sets the minute to 30.datetime_changechanges the minute of the DateTime64 to 45, preserving other components.
When applied to a Date, the function first converts it to DateTime at midnight (00:00:00) before changing the minute.
changeSecond
Changes the second component of a date or date with time. Syntax:date_or_datetime(Date,Date32,DateTime, orDateTime64): A date or date with time.value(Integer): New value for the second component.
date_or_datetime. If the input is a Date, it returns a DateTime. If the input is a Date32, it returns a DateTime64.
Example:
date_changeshows changing the seconds of a Date to 15, which converts it to a DateTime.date_time_changedemonstrates changing the seconds of a DateTime64 to 15, preserving milliseconds.
When applied to a Date, the function implicitly converts it to DateTime at midnight before changing the second component.
addYears
Adds a specified number of years to a date or date with time. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to add years to.num(numeric): Number of years to add.
- Date or date with time with added years. Type matches the input
datetype.
addQuarters
Adds a specified number of quarters to a date, date with time, or string-encoded date/time value. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to add quarters to.num(numeric): Number of quarters to add.
- Date plus
numquarters. Type matches inputdatetype.
addMonths
Adds a specified number of months to a date or date with time. Syntaxdate(Date,Date32,DateTime,DateTime64, orString): Date or date with time to add months to.num(numeric): Number of months to add.
- Date or date with time with added months. Same type as the input
date.
addWeeks
Adds a specified number of weeks to a date, date with time, or string-encoded date/time. Syntaxdate(Date,Date32,DateTime,DateTime64, orString): Date or date with time to add weeks to.num(numeric): Number of weeks to add.
- Date plus
numweeks. Type matches inputdatetype.
If the addition results in a date outside the valid range for the data type, the behavior is undefined.
addDays
Adds a specified number of days to a date, a date with time, or a string-encoded date/time. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to add days to.num(numeric): Number of days to add.
- Date plus
numdays. Type matches inputdatetype.
addHours
Adds a specified number of hours to a date, date with time, or string-encoded date/time. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to add hours to.num(numeric): Number of hours to add.
- Date plus
numhours. Type matches inputdatetype.
add_hours_with_dateadds 12 hours to a Date, resulting in a DateTime.add_hours_with_date_timeadds 12 hours to a DateTime.add_hours_with_date_time_stringadds 12 hours to a string-encoded date/time.
If adding hours results in a date outside the valid range for the data type, the behavior is undefined.
addMinutes
Adds a specified number of minutes to a date, a date with time, or a string-encoded date/time. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to add specified number of minutes to.num(numeric): Number of minutes to add.
num minutes. The return type matches the input type for Date/DateTime/DateTime64, or DateTime64 for string inputs.
Example:
add_minutes_with_dateadds 20 minutes to a Date, resulting in a DateTime.add_minutes_with_date_timeadds 20 minutes to a DateTime.add_minutes_with_date_time_stringadds 20 minutes to a string-encoded date/time, resulting in a DateTime64.
If the addition results in a value outside the bounds of the data type, the behavior is undefined.
addSeconds
Adds a specified number of seconds to a date, date with time, or string-encoded date/time. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to add seconds to.num(numeric): Number of seconds to add.
- Date plus
numseconds. Type matches inputdatetype.
add_seconds_with_dateadds 30 seconds to a Date, resulting in a DateTime.add_seconds_with_date_timeadds 30 seconds to a DateTime.add_seconds_with_date_time_stringadds 30 seconds to a string-encoded date/time, resulting in a DateTime64.
Note that when adding seconds to a Date, it automatically converts the result to a DateTime to represent the time component.
addMilliseconds
Adds a specified number of milliseconds to a date with time or a string-encoded date with time. Syntax:date_time(DateTime/DateTime64, orString): Date with time to add specified number of milliseconds to.num(numeric): Number of milliseconds to add.
- Returns
date_timeplusnummilliseconds.DateTime64.
addMicroseconds
Adds a specified number of microseconds to a date with time or a string-encoded date with time. Syntaxdate_time(DateTimeorDateTime64,String) — Date with time to add specified number of microseconds to.num(numeric) — Number of microseconds to add.
- Returns
date_timeplusnummicroseconds. (DateTime64).
addNanoseconds
Adds a specified number of nanoseconds to a date with time or a string-encoded date with time. Syntaxdate_time(DateTimeorDateTime64,String): Date with time to add specified number of nanoseconds to.num(Int,Float): Number of nanoseconds to add.
- Returns
date_timeplusnumnanoseconds. (DateTime64).
interval_1(IntervalorTupleofInterval): First interval or tuple of intervals.interval_2(Interval): Second interval to be added.
- A tuple of intervals.
TupleofInterval.
Intervals of the same type will be combined into a single interval. For instance, if
toIntervalDay(1) and toIntervalDay(2) are passed, the result will be (3) rather than (1,1).addTupleOfIntervals
Consecutively adds a tuple of intervals to a Date or a DateTime. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.intervals(Tuple(Interval)): Tuple of intervals to add to date.
- Date with added intervals. Same type as the input
date.
The intervals are applied in the order they appear in the tuple. The result may differ if you change the order of the intervals.
subtractYears
Subtracts a specified number of years from a date or date with time. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to subtract years from.num(numeric): Number of years to subtract.
- Date or date with time with the specified number of years subtracted. Same type as the input
date.
subtractYears function can be used to calculate dates for planning events or analyzing historical data for a taco business.
subtractQuarters
Subtracts a specified number of quarters from a date, a date with time, or a string-encoded date/time value. Syntaxdate(Date,Date32,DateTime,DateTime64, orString): Date or date with time to subtract quarters from.num(numeric): Number of quarters to subtract.
- Date minus the specified number of quarters. Returns the same type as the input
date.
date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to subtract months from.num(numeric): Number of months to subtract.
- Date or date with time with the specified number of months subtracted. Returns the same type as the input
date.
subtractWeeks
Subtracts a specified number of weeks from a date, date with time, or string-encoded date/time value. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to subtract weeks from.num(numeric): Number of weeks to subtract.
- Date minus the specified number of weeks. Type matches the input
datetype.
date.
subtractDays
Subtracts a specified number of days from a date, date with time, or string-encoded date/time value. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to subtract days from.num(numeric): Number of days to subtract.
- Date or date with time with the specified number of days subtracted. Same type as the input
date.
subtractHours
Subtracts a specified number of hours from a date, date with time, or string-encoded date/time value. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to subtract hours from.num(numeric): Number of hours to subtract.
- Date minus
numhours. Type matches inputdatetype. If input isDate, returnsDateTime. If input isDate32, returnsDateTime64.
date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to subtract minutes from.num(numeric): Number of minutes to subtract.
- Date or date with time with the specified number of minutes subtracted. The return type matches the input type of
date.
subtract_minutes_with_datetimesubtracts 30 minutes from the DateTime ‘2024-01-01 00:00:00’.subtract_minutes_with_stringsubtracts 30 minutes from the string-encoded date ‘2024-01-01 00:00:00’.
Note that when using a string input, the function automatically converts it to a DateTime64 value before performing the subtraction.
subtractSeconds
Subtracts a specified number of seconds from a date, date with time, or string-encoded date/time value. Syntax:date(Date,Date32,DateTime,DateTime64, orString) — Date or date with time to subtract seconds from.num(numeric) — Number of seconds to subtract.
- Date or date with time with
numseconds subtracted. Type matches inputdatetype.
subtractMilliseconds
Subtracts a specified number of milliseconds from a date with time or a string-encoded date with time. Syntax:date_time(DateTimeorDateTime64,String): Date with time to subtract specified number of milliseconds from.num(numeric): Number of milliseconds to subtract.
- Returns
date_timeminusnummilliseconds.DateTime64.
subtractMicroseconds
Subtracts a specified number of microseconds from a date with time or a string-encoded date with time. Syntax:date_time(DateTime,DateTime64,String): Date with time to subtract specified number of microseconds from.num(Integer,Float): Number of microseconds to subtract.
- Returns
date_timeminusnummicroseconds. (DateTime64).
date_time(DateTimeorDateTime64orString): Date with time to subtract specified number of nanoseconds from.num(numeric): Number of nanoseconds to subtract.
- Returns
date_timeminusnumnanoseconds. (DateTime64).
subtractInterval
Subtracts a time interval from a date, date with time, or string-encoded date/time value. Syntax:date(Date,Date32,DateTime,DateTime64, orString): Date or date with time to subtract the interval from.interval(Interval): Interval to subtract.
- Date or date with time with the interval subtracted. (
Date,Date32,DateTime, orDateTime64).
If the subtraction results in a value outside the bounds of the data type, the result is undefined.
subtractTupleOfIntervals
Consecutively subtracts a tuple of intervals from a Date or a DateTime. Syntax:date(Date,Date32,DateTime, orDateTime64): Date or date with time.intervals(Tuple(Interval)): Tuple of intervals to subtract from date.
- Date with subtracted intervals. The return type matches the type of
date.
timeSlots
Returns an array of time slots for a given time interval. Syntax:startTime(DateTimeorDateTime64): The start time of the interval.duration(UInt32forDateTime,Decimal64forDateTime64): The duration of the interval in seconds.size(UInt32, optional, default: 1800): The size of each time slot in seconds.
DateTime or DateTime64 values representing the time slots.
Example:
The function returns time slots rounded down to the specified size. It’s useful for analyzing events or creating time-based schedules, like delivery windows or appointment slots.
formatDateTime
Formats a date or date with time according to the given format string. Syntaxdate(Date,DateTime, orDateTime64): Date or date with time.format(String): Format string.timezone(String, optional): Timezone for the returned value.
- Formatted date/time string. (
String)
format string may contain the following specifiers:
| Specifier | Description | Example |
|---|---|---|
%Y | Year (4 digits) | 2023 |
%m | Month (01-12) | 05 |
%d | Day of the month (01-31) | 15 |
%H | Hour in 24h format (00-23) | 13 |
%M | Minute (00-59) | 30 |
%S | Second (00-59) | 45 |
%f | Microsecond (000000-999999) | 123456 |
%w | Weekday as number (0-6, 0 is Sunday) | 2 |
%b | Abbreviated month name | May |
%B | Full month name | May |
%a | Abbreviated weekday name | Tue |
%A | Full weekday name | Tuesday |
formatDateTimeInJodaSyntax
Formats a date and time value according to the specified Joda-style format string. Syntax:date_or_datetime(Date,DateTime, orDateTime64): Date or date with time to format.format(String): Format string in Joda syntax.timezone(String, optional): Timezone for the returned value.
- A formatted string representation of the date and time. (
String).
formatDateTime, but uses Joda-style format patterns instead of MySQL-style patterns. For a full list of supported format patterns, refer to the Joda-Time documentation.
Taco-themed Example:
dateName
Returns the specified part of a date as a string. Syntax:date_part(String) — The part of the date to return. Possible values: ‘year’, ‘quarter’, ‘month’, ‘week’, ‘dayofyear’, ‘day’, ‘weekday’, ‘hour’, ‘minute’, ‘second’.date(Date,Date32,DateTime, orDateTime64) — The date to extract the part from.timezone(String, optional) — Optional timezone for the returned value.
String).
Example:
yearreturns the year as a string.monthreturns the full name of the month.dayreturns the day of the month as a string.
monthName
Returns the name of the month for a given date or date with time. Syntax:date(Date,DateTime, orDateTime64): Date or date with time.
- The name of the month.
String
fromUnixTimestamp
Converts a Unix timestamp to a date and time. Syntax:- FROM_UNIXTIME()
unix_timestamp(Integer): Unix timestamp.format(String, optional): Format string for the returned value.timezone(String, optional): Timezone for the returned value.
- When called with a single argument, returns a
DateTimevalue. - When called with two or three arguments, returns a
Stringformatted according to the specifiedformat.
- The first column converts the Unix timestamp to a DateTime.
- The second column formats the timestamp as a string in Los Angeles time zone.
fromUnixTimestampInJodaSyntax().
This function is similar to toDateTime() when called with a single argument.
The single-argument form is an alias for FROM_UNIXTIME().
fromUnixTimestampInJodaSyntax
Converts a Unix timestamp to a formatted date and time string using Joda-Time syntax. Syntax:unix_timestamp(Integer,Date,Date32,DateTime, orDateTime64): Unix timestamp to convert.format(String): Format string in Joda-Time syntax.timezone(String, optional): Optional timezone for the returned value.
- Formatted date and time string. (
String).
The function uses Joda-Time syntax for formatting, which differs from MySQL’s datetime format style. For MySQL-style formatting, use the
fromUnixTimestamp function instead.toModifiedJulianDay
Converts a Proleptic Gregorian calendar date in text form YYYY-MM-DD to a Modified Julian Day number. Syntaxdate(StringorFixedString): Date in text form.
- Modified Julian Day number. Type:
Int32.
This function supports dates 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.
toModifiedJulianDayOrNull
Converts a date string to a Modified Julian Day number, returning NULL if the conversion fails. Syntaxdate(StringorFixedString): Date string in ‘YYYY-MM-DD’ format.
- The Modified Julian Day number, or NULL if the input is invalid. (
Nullable(Int32))
The function supports dates from 0000-01-01 to 9999-12-31. Any date outside this range or in an invalid format will return NULL.
fromModifiedJulianDay
Converts a Modified Julian Day number to a date in the Proleptic Gregorian calendar. Syntax:day(Integer): Modified Julian Day number.
- A date in ‘YYYY-MM-DD’ format.
String.
The Modified Julian Day (MJD) is a standard count of days, where MJD 0 corresponds to November 17, 1858. It’s commonly used in astronomy and space science.
fromModifiedJulianDayOrNull
Converts a Modified Julian Day number to a date in the Proleptic Gregorian calendar, returning NULL instead of raising an exception for invalid inputs. Syntaxday(Integer): Modified Julian Day number.
- A date in the format ‘YYYY-MM-DD’.
Nullable(String). - Returns NULL if the day number is outside the supported range (-678941 to 2973483).
toUTCTimestamp
Converts a DateTime or DateTime64 value from a specified time zone to UTC timezone. Syntaxtime_val(DateTimeorDateTime64): A DateTime or DateTime64 value to convert.time_zone(String): The source time zone oftime_val.
- The input time converted to UTC. [
DateTime] or [DateTime64]
This function is useful for normalizing timestamps from different time zones to a common UTC reference. It’s particularly handy when dealing with data from multiple geographic locations or when preparing data for timezone-agnostic analysis.
fromUTCTimestamp
Converts a DateTime or DateTime64 value from UTC timezone to another specified timezone. Syntaxtime_val(DateTimeorDateTime64): A DateTime or DateTime64 value in UTC timezone.time_zone(String): Target timezone name (e.g., ‘America/New_York’, ‘Europe/London’).
DateTimeorDateTime64value adjusted to the specified timezone.
This function is useful for converting UTC timestamps to local timezones, which is common when working with data from different geographical regions or standardizing timestamps for display purposes.
UTCTimestamp
Returns the current date and time at the moment of query analysis in the UTC timezone. The function is a constant expression. Syntax:- UTC_timestamp
- The current date and time at the moment of query analysis.
DateTime.
now('UTC'). It was added for MySQL compatibility, but now('UTC') is the preferred usage in ClickHouse.
The returned timestamp is fixed at the moment of query analysis, not execution. This means that for long-running queries, the timestamp will not change during query execution.
timeDiff
Calculates the difference between two date or datetime values in seconds. Syntax:first_datetime(DateTimeorDateTime64): The first date/time value.second_datetime(DateTimeorDateTime64): The second date/time value.
- The difference between the two dates/times in seconds. (
Int64)
timeDiff calculates the number of seconds between two taco order times, showing that 30 minutes (1800 seconds) elapsed between orders.
This function is equivalent to
dateDiff('second', first_datetime, second_datetime) and was added for MySQL compatibility. Using dateDiff is generally preferred in ClickHouse.