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
SELECT
toDateTime('2016-06-15 23:00:00') AS time,
toDate(time) AS date_local,
toDate(time, 'Asia/Yekaterinburg') AS date_yekat,
toString(time, 'US/Samoa') AS time_samoa
┌────────────────time─┬─date_local─┬─date_yekat─┬─time_samoa──────────┐
│ 2016-06-15 23:00:00 │ 2016-06-15 │ 2016-06-16 │ 2016-06-15 09:00:00 │
└─────────────────────┴────────────┴────────────┴─────────────────────┘
makeDate
Creates a Date
- from a year, month and day argument, or
- from a year and day of year argument.
Syntax
makeDate(year, month, day);
makeDate(year, day_of_year);
Alias:
MAKEDATE(year, month, day);
MAKEDATE(year, day_of_year);
Arguments
year
— Year. Integer, Float or Decimal.month
— Month. Integer, Float or Decimal.day
— Day. Integer, Float or Decimal.day_of_year
— Day of the year. Integer, Float or Decimal.
Returned value
- A date created from the arguments. Date.
Example
Create a Date from a year, month and day:
SELECT makeDate(2023, 2, 28) AS Date;
Result:
┌───────date─┐
│ 2023-02-28 │
└────────────┘
Create a Date from a year and day of year argument:
SELECT makeDate(2023, 42) AS Date;
Result:
┌───────date─┐
│ 2023-02-11 │
└────────────┘
makeDate32
Creates a date of type Date32 from a year, month, day (or optionally a year and a day).
Syntax
makeDate32(year, [month,] day)
Arguments
year
— Year. Integer, Float or Decimal.month
— Month (optional). Integer, Float or Decimal.day
— Day. Integer, Float or Decimal.
If month
is omitted then day
should take a value between 1
and 365
, otherwise it should take a value between 1
and 31
.
Returned values
- A date created from the arguments. Date32.
Examples
Create a date from a year, month, and day:
Query:
SELECT makeDate32(2024, 1, 1);
Result:
2024-01-01
Create a Date from a year and day of year:
Query:
SELECT makeDate32(2024, 100);
Result:
2024-04-09
makeDateTime
Creates a DateTime from a year, month, day, hour, minute and second argument.
Syntax
makeDateTime(year, month, day, hour, minute, second[, timezone])
Arguments
year
— Year. Integer, Float or Decimal.month
— Month. Integer, Float or Decimal.day
— Day. Integer, Float or Decimal.hour
— Hour. Integer, Float or Decimal.minute
— Minute. Integer, Float or Decimal.second
— Second. Integer, Float or Decimal.timezone
— Timezone for the returned value (optional).
Returned value
- A date with time created from the arguments. DateTime.
Example
SELECT makeDateTime(2023, 2, 28, 17, 12, 33) AS DateTime;
Result:
┌────────────DateTime─┐
│ 2023-02-28 17:12:33 │
└─────────────────────┘
makeDateTime64
Creates a DateTime64 data type value from its components: year, month, day, hour, minute, second. With optional sub-second precision.
Syntax
makeDateTime64(year, month, day, hour, minute, second[, precision])
Arguments
year
— Year (0-9999). Integer, Float or Decimal.month
— Month (1-12). Integer, Float or Decimal.day
— Day (1-31). Integer, Float or Decimal.hour
— Hour (0-23). Integer, Float or Decimal.minute
— Minute (0-59). Integer, Float or Decimal.second
— Second (0-59). Integer, Float or Decimal.precision
— Optional precision of the sub-second component (0-9). Integer.
Returned value
- A date and time created from the supplied arguments. DateTime64.
Example
SELECT makeDateTime64(2023, 5, 15, 10, 30, 45, 779, 5);
┌─makeDateTime64(2023, 5, 15, 10, 30, 45, 779, 5)─┐
│ 2023-05-15 10:30:45.00779 │
└─────────────────────────────────────────────────┘
timestamp
Converts the first argument 'expr' to type DateTime64(6). If a second argument 'expr_time' is provided, it adds the specified time to the converted value.
Syntax
timestamp(expr[, expr_time])
Alias: TIMESTAMP
Arguments
Examples
SELECT timestamp('2023-12-31') as ts;
Result:
┌─────────────────────────ts─┐
│ 2023-12-31 00:00:00.000000 │
└────────────────────────────┘
SELECT timestamp('2023-12-31 12:00:00', '12:00:00.11') as ts;
Result:
┌─────────────────────────ts─┐
│ 2024-01-01 00:00:00.110000 │
└────────────────────────────┘
Returned value
- DateTime64(6)
timeZone
Returns the timezone of the current session, i.e. the value of setting session_timezone. 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
timeZone()
Alias: timezone
.
Returned value
- Timezone. String.
Example
SELECT timezone()
Result:
┌─timezone()─────┐
│ America/Denver │
└────────────────┘
See also
serverTimeZone
Returns the timezone of the server, i.e. the value of setting timezone. 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()
Alias: serverTimezone
.
Returned value
- Timezone. String.
Example
SELECT serverTimeZone()
Result:
┌─serverTimeZone()─┐
│ UTC │
└──────────────────┘
See also
toTimeZone
Converts a date or date with time to the specified time zone. Does not change the internal value (number of unix seconds) of the data, only the value's time zone attribute and the value's string representation changes.
Syntax
toTimezone(value, timezone)
Alias: toTimezone
.
Arguments
value
— Time or date and time. DateTime64.timezone
— Timezone for the returned value. String. This argument is a constant, becausetoTimezone
changes the timezone of a column (timezone is an attribute ofDateTime*
types).
Returned value
- Date and time. DateTime.
Example
SELECT toDateTime('2019-01-01 00:00:00', 'UTC') AS time_utc,
toTypeName(time_utc) AS type_utc,
toInt32(time_utc) AS int32utc,
toTimeZone(time_utc, 'Asia/Yekaterinburg') AS time_yekat,
toTypeName(time_yekat) AS type_yekat,
toInt32(time_yekat) AS int32yekat,
toTimeZone(time_utc, 'US/Samoa') AS time_samoa,
toTypeName(time_samoa) AS type_samoa,
toInt32(time_samoa) AS int32samoa
FORMAT Vertical;
Result:
Row 1:
──────
time_utc: 2019-01-01 00:00:00
type_utc: DateTime('UTC')
int32utc: 1546300800
time_yekat: 2019-01-01 05:00:00
type_yekat: DateTime('Asia/Yekaterinburg')
int32yekat: 1546300800
time_samoa: 2018-12-31 13:00:00
type_samoa: DateTime('US/Samoa')
int32samoa: 1546300800
See Also
- formatDateTime - supports non-constant timezone.
- toString - supports non-constant timezone.
timeZoneOf
Returns the timezone name of DateTime or DateTime64 data types.
Syntax
timeZoneOf(value)
Alias: timezoneOf
.
Arguments
value
— Date and time. DateTime or DateTime64.
Returned value
- Timezone name. String.
Example
SELECT timezoneOf(now());
Result:
┌─timezoneOf(now())─┐
│ Etc/UTC │
└───────────────────┘
timeZoneOffset
Returns the timezone offset in seconds from UTC. The function daylight saving time and historical timezone changes at the specified date and time into account. The IANA timezone database is used to calculate the offset.
Syntax
timeZoneOffset(value)
Alias: timezoneOffset
.
Arguments
value
— Date and time. DateTime or DateTime64.
Returned value
- Offset from UTC in seconds. Int32.
Example
SELECT toDateTime('2021-04-21 10:20:30', 'America/New_York') AS Time, toTypeName(Time) AS Type,
timeZoneOffset(Time) AS Offset_in_seconds, (Offset_in_seconds / 3600) AS Offset_in_hours;
Result:
┌────────────────Time─┬─Type─────────────────────────┬─Offset_in_seconds─┬─Offset_in_hours─┐
│ 2021-04-21 10:20:30 │ DateTime('America/New_York') │ -14400 │ -4 │
└─────────────────────┴──────────────────────────────┴───────────────────┴─────────────────┘
toYear
Returns the year component (AD) of a date or date with time.
Syntax
toYear(value)
Alias: YEAR
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The year of the given date/time. UInt16.
Example
SELECT toYear(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toYear(toDateTime('2023-04-21 10:20:30'))─┐
│ 2023 │
└───────────────────────────────────────────┘
toQuarter
Returns the quarter (1-4) of a date or date with time.
Syntax
toQuarter(value)
Alias: QUARTER
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The quarter of the year (1, 2, 3 or 4) of the given date/time. UInt8.
Example
SELECT toQuarter(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toQuarter(toDateTime('2023-04-21 10:20:30'))─┐
│ 2 │
└──────────────────────────────────────────────┘
toMonth
Returns the month component (1-12) of a date or date with time.
Syntax
toMonth(value)
Alias: MONTH
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The month of the year (1 - 12) of the given date/time. UInt8.
Example
SELECT toMonth(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toMonth(toDateTime('2023-04-21 10:20:30'))─┐
│ 4 │
└────────────────────────────────────────────┘
toDayOfYear
Returns the number of the day within the year (1-366) of a date or date with time.
Syntax
toDayOfYear(value)
Alias: DAYOFYEAR
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The day of the year (1 - 366) of the given date/time. UInt16.
Example
SELECT toDayOfYear(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toDayOfYear(toDateTime('2023-04-21 10:20:30'))─┐
│ 111 │
└────────────────────────────────────────────────┘
toDayOfMonth
Returns the number of the day within the month (1-31) of a date or date with time.
Syntax
toDayOfMonth(value)
Aliases: DAYOFMONTH
, DAY
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The day of the month (1 - 31) of the given date/time. UInt8.
Example
SELECT toDayOfMonth(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toDayOfMonth(toDateTime('2023-04-21 10:20:30'))─┐
│ 21 │
└─────────────────────────────────────────────────┘
toDayOfWeek
Returns the number of the day within the week of a date or date with time.
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. If the mode argument is omitted, the default mode is 0. The time zone of the date can be specified as the third argument.
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 |
Syntax
toDayOfWeek(t[, mode[, timezone]])
Alias: DAYOFWEEK
.
Arguments
t
- a Date, Date32, DateTime or DateTime64mode
- determines what the first day of the week is. Possible values are 0, 1, 2 or 3. See the table above for the differences.timezone
- optional parameter, it behaves like any other conversion function
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.
Returned value
- The day of the week (1-7), depending on the chosen mode, of the given date/time
Example
The following date is April 21, 2023, which was a Friday:
SELECT
toDayOfWeek(toDateTime('2023-04-21')),
toDayOfWeek(toDateTime('2023-04-21'), 1)
Result:
┌─toDayOfWeek(toDateTime('2023-04-21'))─┬─toDayOfWeek(toDateTime('2023-04-21'), 1)─┐
│ 5 │ 4 │
└───────────────────────────────────────┴──────────────────────────────────────────┘
toHour
Returns the hour component (0-24) 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).
Syntax
toHour(value)
Alias: HOUR
Arguments
value
- a DateTime or DateTime64
Returned value
- The hour of the day (0 - 23) of the given date/time. UInt8.
Example
SELECT toHour(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toHour(toDateTime('2023-04-21 10:20:30'))─┐
│ 10 │
└───── ──────────────────────────────────────┘
toMinute
Returns the minute component (0-59) a date with time.
Syntax
toMinute(value)
Alias: MINUTE
Arguments
value
- a DateTime or DateTime64
Returned value
- The minute of the hour (0 - 59) of the given date/time. UInt8.
Example
SELECT toMinute(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toMinute(toDateTime('2023-04-21 10:20:30'))─┐
│ 20 │
└─────────────────────────────────────────────┘
toSecond
Returns the second component (0-59) of a date with time. Leap seconds are not considered.
Syntax
toSecond(value)
Alias: SECOND
Arguments
value
- a DateTime or DateTime64
Returned value
- The second in the minute (0 - 59) of the given date/time. UInt8.
Example
SELECT toSecond(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toSecond(toDateTime('2023-04-21 10:20:30'))─┐
│ 30 │
└─────────────────────────────────────────────┘
toMillisecond
Returns the millisecond component (0-999) of a date with time.
Syntax
toMillisecond(value)
Arguments*
value
- DateTime or DateTime64
Alias: MILLISECOND
SELECT toMillisecond(toDateTime64('2023-04-21 10:20:30.456', 3))
Result:
┌──toMillisecond(toDateTime64('2023-04-21 10:20:30.456', 3))─┐
│ 456 │
└────────────────────────────────────────────────────────────┘
Returned value
- The millisecond in the minute (0 - 59) of the given date/time. UInt16.
toUnixTimestamp
Converts a string, a date or a date with time to the Unix Timestamp in UInt32
representation.
If the function is called with a string, it accepts an optional timezone argument.
Syntax
toUnixTimestamp(date)
toUnixTimestamp(str, [timezone])
Returned value
- Returns the unix timestamp. UInt32.
Example
SELECT
'2017-11-05 08:07:47' AS dt_str,
toUnixTimestamp(dt_str) AS from_str,
toUnixTimestamp(dt_str, 'Asia/Tokyo') AS from_str_tokyo,
toUnixTimestamp(toDateTime(dt_str)) AS from_datetime,
toUnixTimestamp(toDateTime64(dt_str, 0)) AS from_datetime64,
toUnixTimestamp(toDate(dt_str)) AS from_date,
toUnixTimestamp(toDate32(dt_str)) AS from_date32
FORMAT Vertical;
Result:
Row 1:
──────
dt_str: 2017-11-05 08:07:47
from_str: 1509869267
from_str_tokyo: 1509836867
from_datetime: 1509869267
from_datetime64: 1509869267
from_date: 1509840000
from_date32: 1509840000
The return type of toStartOf*
, toLastDayOf*
, toMonday
, timeSlot
functions described below is determined by the configuration parameter enable_extended_results_for_datetime_functions which is 0
by default.
Behavior for
enable_extended_results_for_datetime_functions = 0
:- Functions
toStartOfYear
,toStartOfISOYear
,toStartOfQuarter
,toStartOfMonth
,toStartOfWeek
,toLastDayOfWeek
,toLastDayOfMonth
,toMonday
returnDate
orDateTime
. - Functions
toStartOfDay
,toStartOfHour
,toStartOfFifteenMinutes
,toStartOfTenMinutes
,toStartOfFiveMinutes
,toStartOfMinute
,timeSlot
returnDateTime
. Though these functions can take values of the extended typesDate32
andDateTime64
as an argument, passing them a time outside the normal range (year 1970 to 2149 forDate
/ 2106 forDateTime
) will produce wrong results.
- Functions
enable_extended_results_for_datetime_functions = 1
:- Functions
toStartOfYear
,toStartOfISOYear
,toStartOfQuarter
,toStartOfMonth
,toStartOfWeek
,toLastDayOfWeek
,toLastDayOfMonth
,toMonday
returnDate
orDateTime
if their argument is aDate
orDateTime
, and they returnDate32
orDateTime64
if their argument is aDate32
orDateTime64
. - Functions
toStartOfDay
,toStartOfHour
,toStartOfFifteenMinutes
,toStartOfTenMinutes
,toStartOfFiveMinutes
,toStartOfMinute
,timeSlot
returnDateTime
if their argument is aDate
orDateTime
, and they returnDateTime64
if their argument is aDate32
orDateTime64
.
- Functions
toStartOfYear
Rounds down a date or date with time to the first day of the year. Returns the date as a Date
object.
Syntax
toStartOfYear(value)
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The first day of the year of the input date/time. Date.
Example
SELECT toStartOfYear(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toStartOfYear(toDateTime('2023-04-21 10:20:30'))─┐
│ 2023-01-01 │
└──────────────────────────────────────────────────┘
toStartOfISOYear
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 https://en.wikipedia.org/wiki/ISO_week_date.)
Syntax
toStartOfISOYear(value)
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The first day of the year of the input date/time. Date.
Example
SELECT toStartOfISOYear(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toStartOfISOYear(toDateTime('2023-04-21 10:20:30'))─┐
│ 2023-01-02 │
└─────────────────────────────────────────────────────┘
toStartOfQuarter
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. Returns the date.
Syntax
toStartOfQuarter(value)
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The first day of the quarter of the given date/time. Date.
Example
SELECT toStartOfQuarter(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toStartOfQuarter(toDateTime('2023-04-21 10:20:30'))─┐
│ 2023-04-01 │
└─────────────────────────────────────────────────────┘
toStartOfMonth
Rounds down a date or date with time to the first day of the month. Returns the date.
Syntax
toStartOfMonth(value)
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The first day of the month of the given date/time. Date.
Example
SELECT toStartOfMonth(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toStartOfMonth(toDateTime('2023-04-21 10:20:30'))─┐
│ 2023-04-01 │
└───────────────────────────────────────────────────┘
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
toLastDayOfMonth(value)
Alias: LAST_DAY
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The last day of the month of the given date/time=. Date.
Example
SELECT toLastDayOfMonth(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toLastDayOfMonth(toDateTime('2023-04-21 10:20:30'))─┐
│ 2023-04-30 │
└─────────────────────────────────────────────────────┘
toMonday
Rounds down a date or date with time to the nearest Monday. Returns the date.
Syntax
toMonday(value)
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The date of the nearest Monday on or prior to the given date. Date.
Example
SELECT
toMonday(toDateTime('2023-04-21 10:20:30')), /* a Friday */
toMonday(toDate('2023-04-24')), /* already a Monday */
Result:
┌─toMonday(toDateTime('2023-04-21 10:20:30'))─┬─toMonday(toDate('2023-04-24'))─┐
│ 2023-04-17 │ 2023-04-24 │
└─────────────────────────────────────────────┴────────────────────────────────┘
toStartOfWeek
Rounds a date or date with time down to the nearest Sunday or Monday. Returns the date. The mode argument works exactly like the mode argument in function toWeek()
. If no mode is specified, it defaults to 0.
Syntax
toStartOfWeek(t[, mode[, timezone]])
Arguments
t
- a Date, Date32, DateTime or DateTime64mode
- determines the first day of the week as described in the toWeek() functiontimezone
- Optional parameter, it behaves like any other conversion function
Returned value
- The date of the nearest Sunday or Monday on or prior to the given date, depending on the mode. Date.
Example
SELECT
toStartOfWeek(toDateTime('2023-04-21 10:20:30')), /* a Friday */
toStartOfWeek(toDateTime('2023-04-21 10:20:30'), 1), /* a Friday */
toStartOfWeek(toDate('2023-04-24')), /* a Monday */
toStartOfWeek(toDate('2023-04-24'), 1) /* a Monday */
FORMAT Vertical
Result:
Row 1:
──────
toStartOfWeek(toDateTime('2023-04-21 10:20:30')): 2023-04-16
toStartOfWeek(toDateTime('2023-04-21 10:20:30'), 1): 2023-04-17
toStartOfWeek(toDate('2023-04-24')): 2023-04-23
toStartOfWeek(toDate('2023-04-24'), 1): 2023-04-24
toLastDayOfWeek
Rounds a date or date with time up to the nearest Saturday or Sunday. Returns the date.
The mode argument works exactly like the mode argument in function toWeek()
. If no mode is specified, mode is assumed as 0.
Syntax
toLastDayOfWeek(t[, mode[, timezone]])
Arguments
t
- a Date, Date32, DateTime or DateTime64mode
- determines the last day of the week as described in the toWeek functiontimezone
- Optional parameter, it behaves like any other conversion function
Returned value
- The date of the nearest Sunday or Monday on or after the given date, depending on the mode. Date.
Example
SELECT
toLastDayOfWeek(toDateTime('2023-04-21 10:20:30')), /* a Friday */
toLastDayOfWeek(toDateTime('2023-04-21 10:20:30'), 1), /* a Friday */
toLastDayOfWeek(toDate('2023-04-22')), /* a Saturday */
toLastDayOfWeek(toDate('2023-04-22'), 1) /* a Saturday */
FORMAT Vertical
Result:
Row 1:
──────
toLastDayOfWeek(toDateTime('2023-04-21 10:20:30')): 2023-04-22
toLastDayOfWeek(toDateTime('2023-04-21 10:20:30'), 1): 2023-04-23
toLastDayOfWeek(toDate('2023-04-22')): 2023-04-22
toLastDayOfWeek(toDate('2023-04-22'), 1): 2023-04-23
toStartOfDay
Rounds down a date with time to the start of the day.
Syntax
toStartOfDay(value)
Arguments
value
- a Date, Date32, DateTime or DateTime64
Returned value
- The start of the day of the given date/time. DateTime.
Example
SELECT toStartOfDay(toDateTime('2023-04-21 10:20:30'))
Result:
┌─toStartOfDay(toDateTime('2023-04-21 10:20:30'))─┐
│ 2023-04-21 00:00:00 │
└─────────────────────────────────────────────────┘
toStartOfHour
Rounds down a date with time to the start of the hour.
Syntax
toStartOfHour(value)
Arguments
value
- a DateTime or DateTime64
Returned value
- The start of the hour of the given date/time. DateTime.
Example
SELECT
toStartOfHour(toDateTime('2023-04-21 10:20:30')),
toStartOfHour(toDateTime64('2023-04-21', 6))
Result:
┌─toStartOfHour(toDateTime('2023-04-21 10:20:30'))─┬─toStartOfHour(toDateTime64('2023-04-21', 6))─┐
│ 2023-04-21 10:00:00 │ 2023-04-21 00:00:00 │
└──────────────────────────────────────────────────┴──────────────────────────────────────────────┘
toStartOfMinute
Rounds down a date with time to the start of the minute.
Syntax
toStartOfMinute(value)
Arguments
value
- a DateTime or DateTime64
Returned value
- The start of the minute of the given date/time. DateTime.
Example
SELECT
toStartOfMinute(toDateTime('2023-04-21 10:20:30')),
toStartOfMinute(toDateTime64('2023-04-21 10:20:30.5300', 8))
FORMAT Vertical
Result:
Row 1:
──────
toStartOfMinute(toDateTime('2023-04-21 10:20:30')): 2023-04-21 10:20:00
toStartOfMinute(toDateTime64('2023-04-21 10:20:30.5300', 8)): 2023-04-21 10:20:00
toStartOfSecond
Truncates sub-seconds.
Syntax
toStartOfSecond(value, [timezone])
Arguments
value
— Date and time. DateTime64.timezone
— Timezone for the returned value (optional). If not specified, the function uses the timezone of thevalue
parameter. String.
Returned value
- Input value without sub-seconds. DateTime64.
Examples
Query without timezone:
WITH toDateTime64('2020-01-01 10:20:30.999', 3) AS dt64
SELECT toStartOfSecond(dt64);
Result:
┌───toStartOfSecond(dt64)─┐
│ 2020-01-01 10:20:30.000 │
└─────────────────────────┘
Query with timezone:
WITH toDateTime64('2020-01-01 10:20:30.999', 3) AS dt64
SELECT toStartOfSecond(dt64, 'Asia/Istanbul');
Result:
┌─toStartOfSecond(dt64, 'Asia/Istanbul')─┐
│ 2020-01-01 13:20:30.000 │
└────────────────────────────────────────┘
See also
- Timezone server configuration parameter.
toStartOfMillisecond
Rounds down a date with time to the start of the milliseconds.
Syntax
toStartOfMillisecond(value, [timezone])
Arguments
value
— Date and time. DateTime64.timezone
— Timezone for the returned value (optional). If not specified, the function uses the timezone of thevalue
parameter. String.
Returned value
- Input value with sub-milliseconds. DateTime64.
Examples
Query without timezone:
WITH toDateTime64('2020-01-01 10:20:30.999999999', 9) AS dt64
SELECT toStartOfMillisecond(dt64);
Result:
┌────toStartOfMillisecond(dt64)─┐
│ 2020-01-01 10:20:30.999000000 │
└───────────────────────────────┘
Query with timezone:
┌─toStartOfMillisecond(dt64, 'Asia/Istanbul')─┐
│ 2020-01-01 12:20:30.999000000 │
└─────────────────────────────────────────────┘
Result:
┌─toStartOfMillisecond(dt64, 'Asia/Istanbul')─┐
│ 2020-01-01 12:20:30.999 │
└─────────────────────────────────────────────┘
toStartOfMicrosecond
Rounds down a date with time to the start of the microseconds.
Syntax
toStartOfMicrosecond(value, [timezone])
Arguments
value
— Date and time. DateTime64.timezone
— Timezone for the returned value (optional). If not specified, the function uses the timezone of thevalue
parameter. String.
Returned value
- Input value with sub-microseconds. DateTime64.
Examples
Query without timezone:
WITH toDateTime64('2020-01-01 10:20:30.999999999', 9) AS dt64
SELECT toStartOfMicrosecond(dt64);
Result:
┌────toStartOfMicrosecond(dt64)─┐
│ 2020-01-01 10:20:30.999999000 │
└───────────────────────────────┘
Query with timezone:
WITH toDateTime64('2020-01-01 10:20:30.999999999', 9) AS dt64
SELECT toStartOfMicrosecond(dt64, 'Asia/Istanbul');
Result:
┌─toStartOfMicrosecond(dt64, 'Asia/Istanbul')─┐
│ 2020-01-01 12:20:30.999999000 │
└─────────────────────────────────────────────┘
See also
- Timezone server configuration parameter.
toStartOfNanosecond
Rounds down a date with time to the start of the nanoseconds.
Syntax
toStartOfNanosecond(value, [timezone])
Arguments
value
— Date and time. DateTime64.timezone
— Timezone for the returned value (optional). If not specified, the function uses the timezone of thevalue
parameter. String.
Returned value
- Input value with nanoseconds. DateTime64.
Examples
Query without timezone:
WITH toDateTime64('2020-01-01 10:20:30.999999999', 9) AS dt64
SELECT toStartOfNanosecond(dt64);
Result:
┌─────toStartOfNanosecond(dt64)─┐
│ 2020-01-01 10:20:30.999999999 │
└───────────────────────────────┘
Query with timezone:
WITH toDateTime64('2020-01-01 10:20:30.999999999', 9) AS dt64
SELECT toStartOfNanosecond(dt64, 'Asia/Istanbul');
Result:
┌─toStartOfNanosecond(dt64, 'Asia/Istanbul')─┐
│ 2020-01-01 12:20:30.999999999 │
└────────────────────────────────────────────┘
See also
- Timezone server configuration parameter.
toStartOfFiveMinutes
Rounds down a date with time to the start of the five-minute interval.
Syntax
toStartOfFiveMinutes(value)
Arguments
value
- a DateTime or DateTime64
Returned value
- The start of the five-minute interval of the given date/time. DateTime.
Example
SELECT
toStartOfFiveMinutes(toDateTime('2023-04-21 10:17:00')),
toStartOfFiveMinutes(toDateTime('2023-04-21 10:20:00')),
toStartOfFiveMinutes(toDateTime('2023-04-21 10:23:00'))
FORMAT Vertical
Result:
Row 1:
──────
toStartOfFiveMinutes(toDateTime('2023-04-21 10:17:00')): 2023-04-21 10:15:00
toStartOfFiveMinutes(toDateTime('2023-04-21 10:20:00')): 2023-04-21 10:20:00
toStartOfFiveMinutes(toDateTime('2023-04-21 10:23:00')): 2023-04-21 10:20:00
toStartOfTenMinutes
Rounds down a date with time to the start of the ten-minute interval.
Syntax
toStartOfTenMinutes(value)
Arguments
value
- a DateTime or DateTime64
Returned value
- The start of the ten-minute interval of the given date/time. DateTime.
Example
SELECT
toStartOfTenMinutes(toDateTime('2023-04-21 10:17:00')),
toStartOfTenMinutes(toDateTime('2023-04-21 10:20:00')),
toStartOfTenMinutes(toDateTime('2023-04-21 10:23:00'))
FORMAT Vertical
Result:
Row 1:
──────
toStartOfTenMinutes(toDateTime('2023-04-21 10:17:00')): 2023-04-21 10:10:00
toStartOfTenMinutes(toDateTime('2023-04-21 10:20:00')): 2023-04-21 10:20:00
toStartOfTenMinutes(toDateTime('2023-04-21 10:23:00')): 2023-04-21 10:20:00
toStartOfFifteenMinutes
Rounds down the date with time to the start of the fifteen-minute interval.
Syntax
toStartOfFifteenMinutes(value)
Arguments
value
- a DateTime or DateTime64
Returned value
- The start of the fifteen-minute interval of the given date/time. DateTime.
Example
SELECT
toStartOfFifteenMinutes(toDateTime('2023-04-21 10:17:00')),
toStartOfFifteenMinutes(toDateTime('2023-04-21 10:20:00')),
toStartOfFifteenMinutes(toDateTime('2023-04-21 10:23:00'))
FORMAT Vertical
Result:
Row 1:
──────
toStartOfFifteenMinutes(toDateTime('2023-04-21 10:17:00')): 2023-04-21 10:15:00
toStartOfFifteenMinutes(toDateTime('2023-04-21 10:20:00')): 2023-04-21 10:15:00
toStartOfFifteenMinutes(toDateTime('2023-04-21 10:23:00')): 2023-04-21 10:15:00
toStartOfInterval
This function generalizes other toStartOf*()
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)
.
The calculation is performed relative to specific points in time:
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.
If unit 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.
Syntax
toStartOfInterval(value, INTERVAL x unit[, time_zone])
toStartOfInterval(value, INTERVAL x unit[, origin[, time_zone]])
Aliases: time_bucket
, date_bin
.
The second overload emulates TimescaleDB's time_bucket()
function, respectively PostgreSQL's date_bin()
function, e.g.
SELECT toStartOfInterval(toDateTime('2023-01-01 14:45:00'), INTERVAL 1 MINUTE, toDateTime('2023-01-01 14:35:30'));
Result:
┌───toStartOfInterval(...)─┐
│ 2023-01-01 14:44:30 │