You need to enable JavaScript to run this app.
ByteHouse Cloud Data Warehouse Version

ByteHouse Cloud Data Warehouse Version

Copy page
Download PDF
Function reference
Time and date functions
Copy page
Download PDF
Time and date functions

addDate

warning

MySQL-compatible syntax. It is recommended to use in relevant dialects.

adddate returns the date after adding the specified time.
Syntax

adddate(date, INTERVAL expr unit) 
adddate(expr, days)

Type

  • date
    • The date or datetime expression to which time will be added.
    • Date/Date32/Datetime/Datetime64
    • String
  • INTERVAL expr unit
    • The time interval and unit to add.
    • The unit can be: second, minute, hour, day, month, year, minute_second, hour_second, hour_minute, day_second, day_minute, day_hour, year_month
    • String
  • expr
    • The time interval to add.
    • String
    • Int64
  • days
    • The number of days to add.
    • String
    • Int64

Return type
Date
Example
Example 1:

SELECT adddate(date '2024-07-21', INTERVAL '3' day) as result;
┌─────result─┐ 
│ 2024-07-24 │ 
└────────────┘

Example 2:

SELECT adddate(datetime '2024-07-21', INTERVAL '3' day) as result;
┌──────────────result─┐ 
│ 2024-07-24 00:00:00 │ 
└─────────────────────┘

Example 3:

SELECT adddate('2024-07-21', INTERVAL '3' day) as result;
┌──────────────────result─┐ 
│ 2024-07-24 00:00:00.000 │ 
└─────────────────────────┘

Example 4:

SELECT adddate(datetime '2024-07-21', INTERVAL '3' second) as result;
┌──────────────result─┐ 
│ 2024-07-21 00:00:03 │ 
└─────────────────────┘

Example 5:

SELECT adddate(datetime '2024-07-21', INTERVAL '3' minute) as result;
┌──────────────result─┐ 
│ 2024-07-21 00:03:00 │ 
└─────────────────────┘

Example 6:

SELECT adddate(datetime '2024-07-21', INTERVAL '3' hour) as result;
┌──────────────result─┐ 
│ 2024-07-21 03:00:00 │ 
└─────────────────────┘

Example 7:

SELECT adddate(datetime '2024-07-21', INTERVAL '3' month) as result;
┌──────────────result─┐ 
│ 2024-10-21 00:00:00 │ 
└─────────────────────┘

Example 8:

SELECT adddate(datetime '2024-07-21', INTERVAL '3' year) as result;
┌──────────────result─┐ 
│ 2027-07-21 00:00:00 │ 
└─────────────────────┘

Example 9:

SELECT adddate(datetime '2024-07-21', INTERVAL '01:01:10' hour_second) AS result;
┌──────────────result─┐ 
│ 2024-07-21 01:01:10 │ 
└─────────────────────┘

Example 10:

SELECT adddate(datetime '2024-07-21', INTERVAL '00:10' hour_minute) AS result;
┌──────────────result─┐ 
│ 2024-07-21 00:10:00 │ 
└─────────────────────┘

Example 11:

SELECT adddate(datetime '2024-07-21', INTERVAL '1 01:01:10' day_second) AS result;
┌──────────────result─┐ 
│ 2024-07-22 01:01:10 │ 
└─────────────────────┘

Example 12:

SELECT adddate(datetime '2024-07-21', INTERVAL '01:10' minute_second) AS result;
┌──────────────result─┐ 
│ 2024-07-21 00:01:10 │ 
└─────────────────────┘

Example 13:

SELECT adddate(datetime '2024-07-21', INTERVAL '1 01:01' day_minute) AS result;
┌──────────────result─┐ 
│ 2024-07-22 01:01:00 │ 
└─────────────────────┘

Example 14:

SELECT adddate(datetime '2024-07-21', INTERVAL '1 01' day_hour) AS result;
┌──────────────result─┐ 
│ 2024-07-22 01:00:00 │ 
└─────────────────────┘

Example 15:

SELECT adddate(datetime '2024-07-21 12:13:14', INTERVAL '2 2' year_month) AS result;
┌──────────────result─┐ 
│ 2026-09-21 12:13:14 │ 
└─────────────────────┘

Example 16:

SELECT adddate('2024-07-21', '3');
┌─addDays('2024-07-21', '3')─┐ 
│    2024-07-24 00:00:00.000 │ 
└────────────────────────────┘

Example 17:

SELECT adddate('2024-07-21', 3);
┌─addDays('2024-07-21', 3)─┐ 
│  2024-07-24 00:00:00.000 │ 
└──────────────────────────┘

Example 18:

SELECT adddate(datetime '2024-07-21 12:13:14', 3);
┌─addDays(toDateTime('2024-07-21 12:13:14'), 3)─┐ 
│                           2024-07-24 12:13:14 │ 
└───────────────────────────────────────────────┘

Example 19:

SELECT adddate(datetime '2024-07-21 12:13:14', '3');
┌─addDays(toDateTime('2024-07-21 12:13:14'), '3')─┐ 
│                             2024-07-24 12:13:14 │ 
└─────────────────────────────────────────────────┘

Example 20:

SELECT adddate('2024-07-21 12:13:14', 3);
┌─addDays('2024-07-21 12:13:14', 3)─┐ 
│           2024-07-24 12:13:14.000 │ 
└───────────────────────────────────┘

Example 21:

SELECT adddate('2024-07-21 12:13:14', '3');
┌─addDays('2024-07-21 12:13:14', '3')─┐ 
│             2024-07-24 12:13:14.000 │ 
└─────────────────────────────────────┘

addDays

This function adds a date or datetime interval to a date or datetime, then returns the result.
Syntax

addDays(date, interval) 
addDays(datetime, interval)

Parameters

  • date/datetime Date/DateTime
  • interval Date/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2018-01-01') AS date, 
    toDateTime('2018-01-01 00:00:00') AS date_time 
SELECT 
    addDays(date, 1) AS add_days_with_date, 
    addDays(date_time, 1) AS add_days_with_date_time
┌─add_days_with_date─┬─add_days_with_date_time─┐ 
│ 2018-01-02         │ 2018-01-02 00:00:00     │ 
└────────────────────┴─────────────────────────┘

addHours

This function adds a date or datetime interval to a date or datetime, then returns the result.
Syntax

addHours(date, interval) 
addHours(datetime, interval)

Parameters

  • date/datetime Date/DateTime
  • interval Date/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2018-01-01') AS date, 
    toDateTime('2018-01-01 00:00:00') AS date_time 
SELECT 
    addHours(date, 1) AS add_hours_with_date, 
    addHours(date_time, 1) AS add_hours_with_date_time
┌─add_hours_with_date─┬─add_hours_with_date_time─┐ 
│ 2018-01-01 01:00:00 │ 2018-01-01 01:00:00      │ 
└─────────────────────┴──────────────────────────┘

addMinutes

This function adds a date or datetime interval to a date or datetime, then returns the result.
Syntax

addMinutes(date, interval) 
addMinutes(datetime, interval)

Parameters

  • date/datetime Date/DateTime
  • interval Date/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2018-01-01') AS date, 
    toDateTime('2018-01-01 00:00:00') AS date_time 
SELECT 
    addMinutes(date, 1) AS add_minutes_with_date, 
    addMinutes(date_time, 1) AS add_minutes_with_date_time
┌─add_minutes_with_date─┬─add_minutes_with_date_time─┐ 
│ 2018-01-01 00:01:00   │ 2018-01-01 00:01:00        │ 
└───────────────────────┴────────────────────────────┘

addMonths

This function adds a date or datetime interval to a date or datetime, then returns the result. For example:
Syntax

addMonths(date, interval) 
addMonths(datetime, interval)

Parameters

  • date/datetime Date/DateTime
  • interval Date/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2018-01-01') AS date, 
    toDateTime('2018-01-01 00:00:00') AS date_time 
SELECT 
    addMonths(date, 1) AS add_months_with_date, 
    addMonths(date_time, 1) AS add_months_with_date_time
┌─add_months_with_date─┬─add_months_with_date_time─┐ 
│ 2018-02-01           │ 2018-02-01 00:00:00       │ 
└──────────────────────┴───────────────────────────┘

addQuarters

This function adds a date or datetime interval to a date or datetime, then returns the result.
Syntax

addQuarters(date, interval) 
addQuarters(datetime, interval)

Parameters

  • date/datetime Date/DateTime
  • interval Date/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2018-01-01') AS date, 
    toDateTime('2018-01-01 00:00:00') AS date_time 
SELECT 
    addQuarters(date, 1) AS add_quarters_with_date, 
    addQuarters(date_time, 1) AS add_quarters_with_date_time
┌─add_quarters_with_date─┬─add_quarters_with_date_time─┐ 
│ 2018-04-01             │ 2018-04-01 00:00:00         │ 
└────────────────────────┴─────────────────────────────┘

addSeconds

This function adds a date or datetime interval to a date or datetime, then returns the result. For example:
Syntax

addSeconds(date, interval) 
addSeconds(datetime, interval)

Parameters

  • date/datetime Date/DateTime
  • interval Date/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2018-01-01') AS date, 
    toDateTime('2018-01-01 00:00:00') AS date_time 
SELECT 
    addSeconds(date, 1) AS add_seconds_with_date, 
    addSeconds(date_time, 1) AS add_seconds_with_date_time
┌─add_seconds_with_date─┬─add_seconds_with_date_time─┐ 
│ 2018-01-01 00:00:01   │ 2018-01-01 00:00:01        │ 
└───────────────────────┴────────────────────────────┘

addWeeks

The function adds a date or date-time interval to a date or date-time value, then returns the resulting date or date-time.
Syntax

addWeeks(date, interval) 
addWeeks(datetime, interval)

Parameters

  • date/datetime Date or date-time value
  • interval Date or date-time interval

Return value

  • Date or date-time value

Example

WITH 
    toDate('2018-01-01') AS date, 
    toDateTime('2018-01-01 00:00:00') AS date_time 
SELECT 
    addWeeks(date, 1) AS add_weeks_with_date, 
    addWeeks(date_time, 1) AS add_weeks_with_date_time
┌─add_weeks_with_date─┬─add_weeks_with_date_time─┐ 
│ 2018-01-08          │ 2018-01-08 00:00:00      │ 
└─────────────────────┴──────────────────────────┘

addYears

The function adds a date or date-time interval to a date or date-time value, then returns the resulting date or date-time. For example:
Syntax

addYears(date, interval) 
addYears(datetime, interval)

Parameters

  • date/datetime Date or date-time value
  • interval Date or date-time interval

Return value

  • Date or date-time value

Example

WITH 
    toDate('2018-01-01') AS date, 
    toDateTime('2018-01-01 00:00:00') AS date_time 
SELECT 
    addYears(date, 1) AS add_years_with_date, 
    addYears(date_time, 1) AS add_years_with_date_time
┌─add_years_with_date─┬─add_years_with_date_time─┐ 
│ 2019-01-01          │ 2019-01-01 00:00:00      │ 
└─────────────────────┴──────────────────────────┘

addTime

warning

MySQL compatible syntax. It is recommended to use in relevant dialects.

addtime returns the time after adding the specified time, that is, returns the result of expr1 plus expr2.
Syntax

addtime(expr1, expr2)

Parameter types

  • expr1 Base time
    • Date/Date32
    • Datetime/Datetime64
    • Time
    • String
  • expr2 Incremental time
    • Time
    • String

Return type

  • String

Example
Example 1:

SELECT addtime(DATE '2023-07-15', '02:30:30');
┌─addtime(toDate('2023-07-15'), '02:30:30')─┐ 
│                   2023-07-15 02:30:30.000 │ 
└───────────────────────────────────────────┘

Example 2:

SELECT addtime(TIME '12:00:00', '02:30:30');
┌─addtime(toTimeType('12:00:00'), '02:30:30')─┐ 
│                                14:30:30.000 │ 
└─────────────────────────────────────────────┘

Example 3:

SELECT addtime(DATETIME '2023-07-15 14:00:00', '02:30:30');
┌─addtime(toDateTime('2023-07-15 14:00:00'), '02:30:30')─┐ 
│                                2023-07-15 16:30:30.000 │ 
└────────────────────────────────────────────────────────┘

Example 4:

SELECT addtime(TIMESTAMP '2023-07-15 14:00:00', Time'02:30:30');
┌─addtime(toDateTime('2023-07-15 14:00:00'), '02:30:30')─┐ 
│                                2023-07-15 16:30:30.000 │ 
└────────────────────────────────────────────────────────┘

Example 5:

SELECT addtime('2023-07-15 14:00:00', '02:30:30');
┌─addtime('2023-07-15 14:00:00', '02:30:30')─┐ 
│                    2023-07-15 16:30:30.000 │ 
└────────────────────────────────────────────┘

convert_tz

warning

MySQL compatible syntax. It is recommended to use in relevant dialects.

convert_tz function is used to convert a date-time value from one timezone to another, and returns the result.
Syntax

convert_tz(expr, from_tz, to_tz)

Parameters

  • expr
    • Date-time value to be converted
    • Datetime
  • from_tz
    • Original timezone of the date-time value.
  • to_tz
    • Target timezone to convert to.

Return type

  • Datetime

Example
Example 1:

SELECT convert_tz('2023-06-15 09:30:00', '+00:00', '+08:00');
┌─convert_tz('2023-06-15 09:30:00', '+00:00', '+08:00')─┐ 
│                                   2023-06-15 17:30:00 │ 
└───────────────────────────────────────────────────────┘

Example 2:

SELECT convert_tz('2023-12-25 18:45:00', 'UTC', 'CET');
┌─convert_tz('2023-12-25 18:45:00', 'UTC', 'CET')─┐ 
│                             2023-12-25 19:45:00 │ 
└─────────────────────────────────────────────────┘

curdate

warning

MySQL compatible syntax. It is recommended to use in relevant dialects.

curdate function returns the current date. Same as today()
Syntax

curdate()

Return type

  • Date

Example

SELECT curdate();
┌────today()─┐ 
│ 2024-07-25 │ 
└────────────┘

curtime

warning

MySQL compatible syntax. It is recommended to use in relevant dialects.

curtime function returns the current time.
Syntax

curtime()

Return type

  • Time

Example

SELECT curtime();
┌────CURTIME()─┐ 
│ 16:39:50.844 │ 
└──────────────┘

date

warning

MySQL compatible syntax. It is recommended to use in relevant dialects.

date returns the date from a date or date-time expression.
Syntax

DATE(expr)

Parameter types

  • expr
    • Date/Date32
    • Datetime/Datetime64
    • String

Return type

  • Date

Example
Example 1:

SELECT DATE('2023-06-15 08:45:30'::Datetime);
┌─DATE(CAST('2023-06-15 08:45:30', 'Datetime'))─┐ 
│                                    2023-06-15 │ 
└───────────────────────────────────────────────┘

Example 2:

SELECT DATE('2023-06-15 08:45:30'::Date);
┌─DATE(CAST('2023-06-15 08:45:30', 'Date'))─┐ 
│                                2023-06-15 │ 
└───────────────────────────────────────────┘

Example 3:

SELECT DATE('2023-06-15 08:45:30');
┌─DATE('2023-06-15 08:45:30')─┐ 
│                  2023-06-15 │ 
└─────────────────────────────┘

dateDiff

Returns the difference between two dates or date-time values.
Syntax

dateDiff('unit', startdate, enddate, [timezone])

Parameters

  • unit — Type of the result interval. String. Possible values:
    • second
    • minute
    • hour
    • day
    • week
    • month
    • quarter
    • year
  • startdate — The first time value to subtract (minuend). Date or date-time value.
  • enddate — The second time value to subtract (subtrahend). Date or date-time value.
  • timezone — Timezone name (optional). If specified, it applies to both the start date and end date. If not specified, the timezone of the start date and end date is used. If they are not identical, the result is unspecified. String.

Example

SELECT dateDiff('hour', toDateTime('2018-01-01 22:00:00'), toDateTime('2018-01-02 23:00:00'));
┌─dateDiff('hour', toDateTime('2018-01-01 22:00:00'), toDateTime('2018-01-02 23:00:00'))─┐ 
│ 25                                                                                     │ 
└────────────────────────────────────────────────────────────────────────────────────────┘

date_format

warning

MySQL compatible syntax. It is recommended to use in relevant dialects.

Same as formatDateTime()

date_add

Adds a time interval or date interval to the provided date or datetime.
Syntax

date_add(date, value) 
date_add(datetime, value [,timezone])

Parameter

  • datevalue—Date or DateTime to which the interval is added. Date or DateTime.
  • timezone The timezone parameter is only allowed when the first parameter is of DateTime type.

Return value
Date or DateTime obtained by adding value.
Type: Date or DateTime.
Example

SELECT date_add(toDate('2018-01-01'),3);
┌─date_add(toDate('2018-01-01'), 3)─┐ 
│ 2018-01-04                        │ 
└───────────────────────────────────┘
SELECT date_add(toDateTime('2018-01-01 00:00:00'), 3, 'UTC');
┌─date_add(toDateTime('2018-01-01 00:00:00'), 3, 'UTC')─┐ 
│ 2018-01-03 16:00:00                                   │ 
└───────────────────────────────────────────────────────┘

date_sub

Subtracts a time interval or date interval from the provided date or datetime.
Syntax

date_sub(date, value) 
date_sub(datetime, value [,timezone])

Parameter

  • datevalue—Date or DateTime from which the interval is subtracted. Date or DateTime.
  • timezone The timezone parameter is only allowed when the first parameter is of DateTime type.

Return value
Date or DateTime obtained by subtracting value.
Type: Date or DateTime.
Example

SELECT date_sub(toDate('2018-01-01'),3);
┌─date_sub(toDate('2018-01-01'), 3)─┐ 
│ 2017-12-29                        │ 
└───────────────────────────────────┘
SELECT date_sub(toDateTime('2018-01-01 00:00:00'), 3, 'UTC');
┌─date_sub(toDateTime('2018-01-01 00:00:00'), 3, 'UTC')─┐ 
│ 2017-12-28 16:00:00                                   │ 
└───────────────────────────────────────────────────────┘

dateTrunc

Truncates date and time values to the specified date part.
Syntax

dateTrunc(unit, datetime[, timezone])

alias: DATE_TRUNC
Parameter

  • unit — Interval type for the truncation result. unit parameter is case-insensitive. String type. Compatibility description for different unit values is as follows:

    | | | \

    UnitCompatibility
    \
    nanosecondCompatible only with DateTime64
    \
    microsecondCompatible only with DateTime64
    \
    millisecondCompatible only with DateTime64
    \
    secondCompatible with DateTime64, DateTime
    \
    minuteCompatible with DateTime64, DateTime
    \
    hourCompatible with DateTime64, DateTime
    \
    dayCompatible with DateTime64, DateTime, Date
    \
    weekCompatible with DateTime64, DateTime, Date
    \
    monthCompatible with DateTime64, DateTime, Date
    \
    quarterCompatible with DateTime64, DateTime, Date
    \
    yearCompatible with DateTime64, DateTime, Date
  • datetime — Date and time. Date or Date32 or DateTime or DateTime64 type.

  • timezone — Optional, name of the timezone for the returned datetime. If not specified, the function will use the timezone of the datetime parameter. String type.

Return value
Returns the truncated date and time values.

| | | | \

Unit parameterdatetime parameterReturn type
\
year, quarter, month, weekDate32 or DateTime64 or Date or DateTimeDate32 or Date
\
day, hour, minute, secondDate32, DateTime64, Date or DateTimeDateTime64 or DateTime
\
millisecond, microsecondAnyDateTime64
\
nanosecondNonePrecision is 3, 6, or 9

Example
Truncation without timezone
Query:

SELECT now(), dateTrunc('hour', now());

Output:

┌───────────────now()─┬─dateTrunc('hour', now())──┐ 
│ 2020-09-28 10:40:45 │       2020-09-28 10:00:00 │ 
└─────────────────────┴───────────────────────────┘

Truncation with specified timezone
Query:

SELECT now(), dateTrunc('hour', now(), 'Asia/Istanbul');

Output:

┌───────────────now()─┬─dateTrunc('hour', now(), 'Asia/Istanbul')──┐ 
│ 2020-09-28 10:46:26 │                        2020-09-28 13:00:00 │ 
└─────────────────────┴────────────────────────────────────────────┘

day/dayOfMonth

Returns the day of the month from the date.
Syntax

DAY(expr) 
DAYOFMONTH(expr)

Parameter type

  • expr
    • Date/Date32
    • Datetime/Datetime64
    • Time
    • String

Return type

  • UInt8

Example
Example 1:

SELECT DAYOFMONTH('2022-01-22 12:23:09');
┌─DAYOFMONTH('2022-01-22 12:23:09')─┐ 
│                                22 │ 
└───────────────────────────────────┘

Example 2:

SELECT DAYOFMONTH(TIME '17:01:10');
-- use current date 
┌─DAYOFMONTH(toTimeType('17:01:10'))─┐ 
│                                 25 │ 
└────────────────────────────────────┘

Example 3:

SELECT DAY('2022-01-22');
┌─DAY('2022-01-22')─┐ 
│                22 │ 
└───────────────────┘

dayname

tip

MySQL compatible syntax. It is recommended to use in relevant dialects.

dayname Returns the name of the weekday corresponding to the date.
Syntax

dayname(expr)

Parameter type

  • expr
    • Datetime/Datetime64
    • Date/Date32
    • String

Return type

  • String

Example
Example 1:

SELECT dayname(DATETIME '2023-04-21 03:01:00');
┌─dayname(toDateTime('2023-04-21 03:01:00'))─┐ 
│ Friday                                     │ 
└────────────────────────────────────────────┘

Example 2:

SELECT dayname('2023-04-21 03:01:00');
┌─dayname('2023-04-21 03:01:00')─┐ 
│ Friday                         │ 
└────────────────────────────────┘

dayOfWeek

warning

MySQL-compatible syntax. It is recommended to use in relevant dialects.

dayofweek Returns the index value of the day of the week for the given date, with Sunday as 1 as the starting point.
Syntax

dayofweek(expr)

Parameter type

  • expr
    • Datetime/Datetime64
    • Date/Date32
    • String

Return type

  • UInt8

Example
Example 1:

SELECT dayofweek(DATETIME '2024-04-10 20:30:00');
┌─DAYOFWEEK(toDateTime('2024-04-10 20:30:00'))─┐ 
│                                            4 │ 
└──────────────────────────────────────────────┘

Example 2:

SELECT dayofweek(DATE '2022-01-30');
┌─DAYOFWEEK(toDate('2024-01-30'))─┐ 
│                               3 │ 
└─────────────────────────────────┘

Example 3:

SELECT dayofweek('2024-10-21');
┌─DAYOFWEEK('2024-10-21')─┐ 
│                       2 │ 
└─────────────────────────┘

dayOfYear

warning

MySQL-compatible syntax. It is recommended to use in relevant dialects.

dayofyear Returns the number of days in the current year for the specified date, ranging from [1, 366].
Syntax

dayofyear(expr)

Parameter type

  • expr
    • Datetime/Datetime64
    • Date/Date32
    • String

Return type

  • UInt64

Example
Example 1:

SELECT dayofyear(DATETIME '2024-07-21 13:13:13');
┌─DAYOFYEAR(toDateTime('2024-07-21 13:13:13'))─┐ 
│                                          203 │ 
└──────────────────────────────────────────────┘

Example 2:

SELECT dayofyear('2024-07-21');
┌─DAYOFYEAR('2024-07-21')─┐ 
│                     203 │ 
└─────────────────────────┘

extract

warning

MySQL-compatible syntax. It is recommended to use in relevant dialects.

extract Returns the individual part of the date or time, specified by unit, such as year, month, day, hour, minute, and others.
Syntax

extract(unit FROM date)

Parameter type

  • unit
    • Time unit
    • unit can be: second, minute, hour, day, month, year, minute_second, hour_second, hour_minute, day_second, day_minute, day_hour, year_month.
  • date
    • Date or time expression for extracting part
    • Date/Date32/Datetime/Datetime64/Time
    • String

Return type

  • Int64

Example
Example 1:

SELECT extract(second FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│     14 │ 
└────────┘

Example 2:

SELECT extract(minute FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│     13 │ 
└────────┘

Example 3:

SELECT extract(hour FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│     12 │ 
└────────┘

Example 4:

SELECT extract(day FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│     21 │ 
└────────┘

Example 5:

SELECT extract(month FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│      7 │ 
└────────┘

Example 6:

SELECT extract(year FROM timestamp '2024-07-21') as result;
┌─result─┐ 
│   2024 │ 
└────────┘

Example 7:

SELECT extract(year FROM datetime '2024-07-21') as result;
┌─result─┐ 
│   2024 │ 
└────────┘

Example 8:

SELECT extract(year FROM time '12:13:14') as result;
┌─result─┐ 
│   2024 │ 
└────────┘

Example 9:

SELECT extract(minute_second FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│   1314 │ 
└────────┘

Example 10:

SELECT extract(hour_second FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│ 121314 │ 
└────────┘

Example 11:

SELECT extract(hour_minute FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│   1213 │ 
└────────┘

Example 12:

SELECT extract(day_second FROM '2024-07-21 12:13:14') as result;
┌───result─┐ 
│ 21121314 │ 
└──────────┘

Example 13:

sql 
Copy code 
SELECT extract(day_minute FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│ 211213 │ 
└────────┘

Example 14:

SELECT extract(day_hour FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│   2112 │ 
└────────┘

Example 15:

SELECT extract(year_month FROM '2024-07-21 12:13:14') as result;
┌─result─┐ 
│ 202407 │ 
└────────┘

from_days

warning

MySQL-compatible syntax. It is recommended to use in relevant dialects.

from_days Returns the corresponding Date value based on the specified number of days expr.
Syntax

expr

Parameter type

  • expr
    • String
    • UInt/Int

Return type

  • DATE

Example
Example 1:

SELECT from_days(739453);
┌─from_days(739453)─┐ 
│        2024-07-21 │ 
└───────────────────┘

Example 2:

SELECT from_days('739453');
┌─from_days(739453)─┐ 
│        2024-07-21 │ 
└───────────────────┘

from_unixtime

tip

MySQL-compatible syntax. It is recommended to use in relevant dialects.

from_unixtime Returns the value in the specified format based on the Unix timestamp. The format follows the format in the date_format function.
Syntax

from_unixtime(expr[, format])

Parameter type

  • expr
    • String
    • UInt/Int
    • Float/Double
  • format
    • String

Return type

  • String

Example
Example 1:

SELECT from_unixtime('1704733565', '%Y %M %h:%i:%s');
┌─FROM_UNIXTIME('1704733565', '%Y %M %h:%i:%s')─┐ 
│ 2024 January 01:06:05                         │ 
└───────────────────────────────────────────────┘

Example 2:

SELECT from_unixtime(1704733565);
┌─FROM_UNIXTIME(1704733565)─┐ 
│       2024-01-09 01:06:05 │ 
└───────────────────────────┘

formatDateTime

Formats time according to the given format string. The format is a constant expression, so a single result column cannot have multiple formats.
Syntax

formatDateTime(Time, Format[, Timezone])

Parameter

  • Time date/dateTime
  • Format format pattern

Replacement fields
By using replacement fields, you can define a pattern for the result string. The "Example" column displays the formatted result of 2018-01-02 22:33:44.

| | | | \

PlaceholderDescriptionExample
\
%Cyear divided by 100 and truncated to integer (00-99)\
Year divided by 100 and truncated to integer (00-99)20
\
%dday of the month, zero-padded (01-31)\
Day of the month, zero-padded (01-31)02
\
%DShort MM/DD/YY date, equivalent to %m/%d/%y\
Short MM/DD/YY date, equivalent to %m/%d/%y01/02/18
\
%eday of the month, space-padded ( 1-31)\
Day of the month, space-padded (1-31)2
\
%Fshort YYYY-MM-DD date, equivalent to %Y-%m-%d\
Short YYYY-MM-DD date, equivalent to %Y-%m-%d2018-01-02
\
%Hhour in 24h format (00-23)\
Hour in 24h format (00-23)22
\
%Ihour in 12h format (01-12)\
Hour in 12h format (01-12)10
\
%jday of the year (001-366)\
Day of the year (001-366)002
\
%mmonth as a decimal number (01-12)\
Month as a decimal number (01-12)01
\
%Mminute (00-59)\
Minute (00-59)33
\
%nnew-line character (‘’)\
New-line character ('')
\
%pAM or PM designation\
AM or PM designationPM
\
%R24-hour HH:MMtime, equivalent to %H:%M\
24-hour HH:MM time, equivalent to %H:%M22:33
\
%Ssecond (00-59)\
Second (00-59)44\
\
%t\
horizontal-tab character (’)\
Horizontal-tab character ('')
\
%TISO8601 time format (HH:MM:SS), equivalent to %H:%M:%S\
ISO8601 Time format (HH:MM:SS), equivalent to %H:%M:%S22:33:44
\
%uISO8601 weekday as number with Monday as 1 (1-7)\
ISO8601 weekday as a number, Monday is 1 (1-7)2
\
%VISO8601 week number (01-53)\
ISO8601 number of weeks (01-53)01
\
%wweekday as a decimal number with Sunday as 0 (0-6)\
Weekday represented as a decimal number, Sunday is 0 (0-6)2
\
%yYear, last two digits (00-99)\
Year, last two digits (00-99)18
\
%YYear\
year2018
\
%%a % sign%

Return value
Returns the Time and Date value according to the confirmed Format.
Example

SELECT formatDateTime(toDate('2010-01-04'), '%d');
┌─formatDateTime(toDate('2010-01-04'), '%d')─┐ 
│ 04                                         │ 
└────────────────────────────────────────────┘ 

--from_unixtime 
--The function converts a Unix Timestamp to a calendar Date and Time of day. When there is only one Integer Type parameter, its behaviour is the same as toDateTime and returns DateTime Type. - 

--Example 
SELECT FROM_UNIXTIME(423543535);
┌─FROM_UNIXTIME(423543535)─┐ 
│      1983-06-04 10:58:55 │ 
└──────────────────────────┘

When there are two parameters: the first is Integer or DateTime, the second is a constant Format String — its behaviour is the same as formatDateTime and returns String Type.
For example:

SELECT FROM_UNIXTIME(1234334543, '%Y-%m-%d %R:%S') AS DateTime;
┌─DateTime────────────┐ 
│ 2009-02-11 14:42:23 │ 
└─────────────────────┘

hour

Returns the hour in the Date Time, same as toHour().

last_day

warning

MySQL compatible syntax, Recommended to use in relevant dialects.

last_day Returns the last day of the corresponding month in the Date or Date Time.
Syntax

last_day(expr)

Type

  • expr
    • Datetime/Datetime64
    • Date/Date32
    • String

Return Type

  • Date

Example
Example 1:

SELECT last_day('2024-07-21');
┌─toLastDayOfMonth('2024-07-21')─┐ 
│                     2024-07-31 │ 
└────────────────────────────────┘

Example 2:

SELECT last_day(DATETIME '2024-07-21 10:00:00');
┌─toLastDayOfMonth(toDateTime('2024-07-21 10:00:00'))─┐ 
│                                          2024-07-31 │ 
└─────────────────────────────────────────────────────┘

Example 3:

SELECT last_day('2024-07-21 10:00:00');
┌─toLastDayOfMonth('2024-07-21 10:00:00')─┐ 
│                              2024-07-31 │ 
└─────────────────────────────────────────┘

localtime/localtimestamp

Returns the current Date and Time, same as now().

makedate

warning

MySQL compatible syntax, Recommended to use in relevant dialects.

makedate Returns a Date according to the parameters year and dayofyear.
Syntax

makedate(year, dayofyear)

Type

  • year
    • UInt
    • String
  • dayofyear
    • UInt
    • String

Return Type

  • Date

Example
Example 1:

SELECT makedate(2024, 202), makedate(2024, 203);
┌─makeDate(2024, 202)─┬─makeDate(2024, 203)─┐ 
│          2024-07-20 │          2024-07-21 │ 
└─────────────────────┴─────────────────────┘

Example 2:

SELECT makedate('2024', '202'), makedate('2024', '203');
┌─makeDate('2024', '202')─┬─makeDate('2024', '203')─┐ 
│              2024-07-20 │              2024-07-21 │ 
└─────────────────────────┴─────────────────────────┘

maketime

warning

MySQL compatible syntax, Recommended to use in relevant dialects.

maketime Returns a Time according to the parameters hour, minute, and second.
Syntax

maketime(hour, minute, second)

Type

  • hour
    • UInt
    • String
  • minute
    • UInt
    • String
  • second
    • UInt
    • String

Return Type

  • Time

Example
Example 1:

SELECT maketime(07, 21, 10);
┌─makeTime(7, 21, 10)─┐ 
│        07:21:10.000 │ 
└─────────────────────┘

Example 2:

SELECT maketime('07', '21', '30');
┌─makeTime('07', '21', '30')─┐ 
│               07:21:30.000 │ 
└────────────────────────────┘

minute

Returns the minute in the Date Time, same as toMinute().

month

Returns the month in the Date Time, same as toMonth().

monthname

warning

MySQL compatible syntax, Recommended to use in relevant dialects.

monthname Returns the full English name of the month in the Date.
Syntax

monthname(date)

Type

  • date
    • Datetime/Datetime64
    • Date/Date32
    • String

Return Type

  • String

Example
Example 1:

SELECT monthname(DATETIME '2024-07-21 00:00:00');
┌─monthName(toDateTime('2024-07-21 00:00:00'))─┐ 
│ July                                         │ 
└──────────────────────────────────────────────┘

Example 2:

SELECT monthname(DATE'2024-07-21');
┌─monthName(toDate('2024-07-21'))─┐ 
│ July                            │ 
└─────────────────────────────────┘

Example 3:

SELECT monthname('2024-07-21');
┌─monthName('2024-07-21')─┐ 
│ July                    │ 
└─────────────────────────┘

now

Returns the current Date and Time.
Syntax

now()

Return value

  • Current Date and Time.

Type: Date Time.
Example

SELECT now();
┌─now()───────────────┐ 
│ 2021-08-11 14:35:47 │ 
└─────────────────────┘

period_add

warning

MySQL compatible syntax, Recommended to use in relevant dialects.

warning

MySQL compatible syntax, Recommended to use in relevant dialects.

period_add Returns expr1 in Date format and then pushes expr2 months forward.
Syntax

period_add(expr1, expr2)

Type

  • expr1
    • UInt
    • String
  • expr2
    • UInt
    • String

Return Type

  • UInt

Example
Example 1:

SELECT period_add(202401, 6);
┌─period_add(202401, 6)─┐ 
│                202407 │ 
└───────────────────────┘

Example 2:

SELECT period_add('202401', '6');
┌─period_add('202401', '6')─┐ 
│                    202407 │ 
└───────────────────────────┘

period_diff

period_diff Calculates the difference in months between expr1 and expr2.

warning

MySQL compatible syntax, Recommended to use in relevant dialects.

Syntax

period_diff(expr1, expr2)

Type

  • expr1
    • UInt
    • String
  • expr2
    • UInt
    • String

Return Type

  • UInt

Example
Example 1:

SELECT period_diff(202407, 202301);
┌─period_diff(202407, 202301)─┐ 
│                          18 │ 
└─────────────────────────────┘

Example 2:

SELECT period_diff('202407', '202301');
┌─period_diff('202407', '202301')─┐ 
│                              18 │ 
└─────────────────────────────────┘

quarter

Returns the quarter in the Date Time, same as toQuarter().

sec_to_time

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

sec_to_time converts a number of seconds to Time format.
Syntax

sec_to_time(seconds)

Type

  • seconds
    • UInt/Int
    • String

Return type

  • Time

Example
Example 1:

SELECT sec_to_time(7321);
┌─sec_to_time(7321)─┐ 
│      02:02:01.000 │ 
└───────────────────┘

Example 2:

SELECT sec_to_time('7321');
┌─sec_to_time('7321')─┐ 
│        02:02:01.000 │ 
└─────────────────────┘

second

Returns the seconds in the DateTime, same as toSecond().

str_to_date

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

str_to_date converts a String to Date or DateTime type according to the specified Date or Time Format. The Format follows the format in the date_format function.
Syntax

str_to_date(str, format)

Type

  • str
    • String to be converted
    • String
  • format
    • Format
    • String

Return type

  • Datetime

Example
Example 1:

SELECT str_to_date('2024-07-21 12:13:14', '%Y-%m-%d %H:%i:%s') AS result;
┌─────────────────────result─┐ 
│ 2024-07-21 12:13:14.000000 │ 
└────────────────────────────┘

subtractDays

The function subtracts a date or datetime interval from a date or datetime, then returns the date or datetime.
Syntax

subtractDays(date, interval) 
subtractDays(datetime, interval)

Param

  • date/datetimeDate/DateTime
  • intervalDate/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2019-01-01') AS date, 
    toDateTime('2019-01-01 00:00:00') AS date_time 
SELECT 
    subtractDays(date, 1) AS subtract_days_with_date, 
    subtractDays(date_time, 1) AS subtract_days_with_date_time
┌─subtract_days_with_date─┬─subtract_days_with_date_time─┐ 
│ 2018-12-31              │ 2018-12-31 00:00:00          │ 
└─────────────────────────┴──────────────────────────────┘

subtractHours

The function subtracts a date or datetime interval from a date or datetime, then returns the date or datetime.
Syntax

subtractHours(date, interval) 
subtractHours(datetime, interval)

Param

  • date/datetimeDate/DateTime
  • intervalDate/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2019-01-01') AS date, 
    toDateTime('2019-01-01 00:00:00') AS date_time 
SELECT 
    subtractHours(date, 1) AS subtract_hours_with_date, 
    subtractHours(date_time, 1) AS subtract_hours_with_date_time
┌─subtract_hours_with_date─┬─subtract_hours_with_date_time─┐ 
│ 2002-11-25               │ 2018-12-31 23:00:00           │ 
└──────────────────────────┴───────────────────────────────┘

subtractMinutes

The function subtracts a date or datetime interval from a date or datetime, then returns the date or datetime.
Syntax

subtractMinutes(date, interval) 
subtractMinutes(datetime, interval)

Param

  • date/datetimeDate/DateTime
  • intervalDate/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2019-01-01') AS date, 
    toDateTime('2019-01-01 00:00:00') AS date_time 
SELECT 
    subtractMinutes(date, 1) AS subtract_minutes_with_date, 
    subtractMinutes(date_time, 1) AS subtract_minutes_with_date_time
┌─subtract_minutes_with_date─┬─subtract_minutes_with_date_time─┐ 
│ 2012-08-04                 │ 2018-12-31 23:59:00             │ 
└────────────────────────────┴─────────────────────────────────┘

subtractMonths

The function subtracts a date or datetime interval from a date or datetime, then returns the date or datetime.
Syntax

subtractMonths(date, interval) 
subtractMonths(datetime, interval)

Param

  • date/datetimeDate/DateTime
  • intervalDate/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2019-01-01') AS date, 
    toDateTime('2019-01-01 00:00:00') AS date_time 
SELECT 
    subtractMonths(date, 1) AS subtract_months_with_date, 
    subtractMonths(date_time, 1) AS subtract_months_with_date_time
┌─subtract_months_with_date─┬─subtract_months_with_date_time─┐ 
│ 2018-12-01                │ 2018-12-01 00:00:00            │ 
└───────────────────────────┴────────────────────────────────┘

subtractQuarters

The function subtracts a date or datetime interval from a date or datetime, then returns the date or datetime.
Syntax

subtractQuarters(date, interval) 
subtractQuarters(datetime, interval)

Param

  • date/datetimeDate/DateTime
  • intervalDate/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2019-01-01') AS date, 
    toDateTime('2019-01-01 00:00:00') AS date_time 
SELECT 
    subtractQuarters(date, 1) AS subtract_quarters_with_date, 
    subtractQuarters(date_time, 1) AS subtract_quarters_with_date_time
┌─subtract_quarters_with_date─┬─subtract_quarters_with_date_time─┐ 
│ 2018-10-01                  │ 2018-10-01 00:00:00              │ 
└─────────────────────────────┴──────────────────────────────────┘

subtractSeconds

The function subtracts a date or datetime interval from a date or datetime, then returns the date or datetime.
Syntax

subtractSeconds(date, interval) 
subtractSeconds(datetime, interval)

Param

  • date/datetimeDate/DateTime
  • intervalDate/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2019-01-01') AS date, 
    toDateTime('2019-01-01 00:00:00') AS date_time 
SELECT 
    subtractSeconds(date, 1) AS subtract_seconds_with_date, 
    subtractSeconds(date_time, 1) AS subtract_seconds_with_date_time
┌─subtract_seconds_with_date─┬─subtract_seconds_with_date_time─┐ 
│ 2012-10-02                 │ 2018-12-31 23:59:59             │ 
└────────────────────────────┴─────────────────────────────────┘

subtractWeeks

The function subtracts a date or datetime interval from a date or datetime, then returns the date or datetime.
Syntax

subtractWeeks(date, interval) 
subtractWeeks(datetime, interval)

Param

  • date/datetimeDate/DateTime
  • intervalDate/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2019-01-01') AS date, 
    toDateTime('2019-01-01 00:00:00') AS date_time 
SELECT 
    subtractWeeks(date, 1) AS subtract_weeks_with_date, 
    subtractWeeks(date_time, 1) AS subtract_weeks_with_date_time
┌─subtract_weeks_with_date─┬─subtract_weeks_with_date_time─┐ 
│ 2018-12-25               │ 2018-12-25 00:00:00           │ 
└──────────────────────────┴───────────────────────────────┘

subtractYears

The function subtracts a date or DateTime interval from a date or DateTime, then returns a date or DateTime.
Syntax

subtractYears(date, interval) 
subtractYears(datetime, interval)

Param

  • date/datetimeDate/DateTime
  • intervalDate/DateTime

Return value

  • Date/DateTime

Example

WITH 
    toDate('2019-01-01') AS date, 
    toDateTime('2019-01-01 00:00:00') AS date_time 
SELECT 
    subtractYears(date, 1) AS subtract_years_with_date, 
    subtractYears(date_time, 1) AS subtract_years_with_date_time
┌─subtract_years_with_date─┬─subtract_years_with_date_time─┐ 
│ 2018-01-01               │ 2018-01-01 00:00:00           │ 
└──────────────────────────┴───────────────────────────────┘

subtime

warning

MySQL-compatible syntax, Recommended for use in relevant dialects.

subtime Returns the time after expr1 minus expr2.
Syntax

subtime(expr1, expr2)

Type

  • expr1
    • Expression from which Time will be subtracted
    • Datetime
  • expr2
    • Time to subtract
    • String

Return type

  • Datetime

Example
Example 1:

SELECT subtime(date '2024-07-21', '0:1:1');
┌─subtime(toDate('2024-07-21'), '0:1:1')─┐ 
│                2024-07-20 23:58:59.000 │ 
└────────────────────────────────────────┘

Example 2:

SELECT subtime(datetime '2024-07-21 12:13:14', '0:1:1');
┌─subtime(toDateTime('2024-07-21 12:13:14'), '0:1:1')─┐ 
│                             2024-07-21 12:12:13.000 │ 
└─────────────────────────────────────────────────────┘

Example 3:

SELECT subtime(time '12:13:14', '0:1:1');
┌─subtime(toTimeType('12:13:14'), '0:1:1')─┐ 
│                             12:12:13.000 │ 
└──────────────────────────────────────────┘

Example 4:

SELECT subtime('2024-07-21 12:13:14', '0:1:1');
┌─subtime('2024-07-21 12:13:14', '0:1:1')─┐ 
│                 2024-07-21 12:12:13.000 │ 
└─────────────────────────────────────────┘

sysdate

Returns the current system time, same as now().

time

warning

MySQL-compatible syntax, Recommended for use in relevant dialects.

Returns the time in expr.
Syntax

time(expr[, scale])

Type

  • expr
    • Expression indicating the Time to extract
    • String
    • Datetime/Datetime64
  • scale
    • Indicates the decimal precision of the Goal Time
    • UInt8

Return type

  • Time

Example
Example 1:

SELECT time('2024-07-21 12:13:14');
┌─TIME('2024-07-21 12:13:14')─┐ 
│                12:13:14.000 │ 
└─────────────────────────────┘

Example 2:

SELECT time('2024-07-21 12:13:14', 0);
┌─TIME('2024-07-21 12:13:14', 0)─┐ 
│                       12:13:14 │ 
└────────────────────────────────┘

Example 3:

SELECT time(Datetime64'2024-07-21 12:13:14.123456', 3);
┌─TIME(toDateTime64('2024-07-21 12:13:14.123456'), 3)─┐ 
│                                        12:13:14.123 │ 
└─────────────────────────────────────────────────────┘

timeDiff

warning

MySQL-compatible syntax, Recommended for use in relevant dialects.

Returns the time difference between two inputs, same as subtime().

timestamp

warning

MySQL-compatible syntax, Recommended for use in relevant dialects.

timestamp Returns the date or DateTime represented by expr.
Syntax

timestamp(expr)

Type

  • expr
    • Expression to convert to Timestamp
    • Date/Date32
    • String

Return type

  • Datetime

Example
Example 1:

SELECT timestamp(date '2024-07-21');
┌─timestamp(toDate('2024-07-21'))─┐ 
│      2024-07-21 00:00:00.000000 │ 
└─────────────────────────────────┘

Example 2:

SELECT timestamp('2024-07-21');
┌────timestamp('2024-07-21')─┐ 
│ 2024-07-21 00:00:00.000000 │ 
└────────────────────────────┘

timestampAdd

warning

MySQL-compatible syntax, Recommended for use in relevant dialects.

timestampadd Adds interval to the date or DateTime expression datetime_expr. The unit of interval is specified by unit. The value of unit can be: second, minute, hour, day, week, month, quarter, or year.
Syntax

timestampadd(unit, interval, datetime_expr)

Type

  • unit
    • Time unit
  • interval
    • Time interval
    • String
    • UInt64
  • datetime_expr
    • Date or DateTime Expression
    • String
    • Date/Date32/Datetime/Datetime64/Time

Return type

  • Datetime

Example
Example 1:

SELECT timestampadd(second, '1', datetime '2024-07-21 12:13:14') AS result;
┌──────────────result─┐ 
│ 2024-07-21 12:13:15 │ 
└─────────────────────┘

Example 2:

SELECT timestampadd(second, '1', date '2024-07-21') AS result;
┌──────────────result─┐ 
│ 2024-07-21 00:00:01 │ 
└─────────────────────┘

timestampDiff

warning

MySQL-compatible syntax, Recommended for use in relevant dialects.

timestampdiff Returns the result after datetime_expr2 minus datetime_expr1 for date or DateTime expression, with the unit specified by unit. The value of unit can be: second, minute, hour, day, week, month, quarter, or year.
Syntax

timestampdiff(unit, datetime_expr1, datetime_expr2)

Type

  • unit
    • Time unit
    • String
  • datetime_expr1
    • Start date or DateTime Expression to calculate
    • Date/Date32/Datetime/Datetime64
  • datetime_expr2
    • End date or DateTime Expression to calculate
    • Date/Date32/Datetime/Datetime64
    • String

Return type

  • Int64

Example
Example 1:

SELECT timestampdiff(second, datetime '2024-07-21 10:12:13', datetime '2024-07-21 12:13:14') AS result;
┌─result─┐ 
│   7261 │ 
└────────┘

Example 2:

SELECT timestampdiff(minute, '2024-07-21 10:12:13', '2024-07-21 12:13:14');
┌─timestampdiff('minute', '2024-07-21 10:12:13', '2024-07-21 12:13:14')─┐ 
│                                                                   121 │ 
└───────────────────────────────────────────────────────────────────────┘

timeSlot

Round the Time to the nearest half hour.
This function is unique to Yandex.Metrica, because if the tracking tag shows that the consecutive page views of a single user differ by strictly more than half an hour in time, then half an hour is the minimum time for splitting a session into two sessions. This means that you can search for the page views contained in the corresponding session using the triplet (tag ID, user ID, and time frame).
Syntax

timeSlot(datetime)

Param

  • datetime

Return value

  • DateTime

Example

SELECT timeSlot(toDateTime('2018-01-01 01:01:01'))
┌─timeSlot(toDateTime('2018-01-01 01:01:01'))─┐ 
│ 2018-01-01 01:00:00                         │ 
└─────────────────────────────────────────────┘

timeSlots

For a time interval starting from "Start Time" and lasting "Duration" seconds, it returns an array of time points consisting of points within that interval, rounded to "Size" (in seconds). "Size" is an optional param: a constant UInt32, default setting is 1800.
This is necessary for searching page views in the corresponding session.
Syntax

timeSlots(StartTime, Duration,[, Size])

Param

  • StartTime: start time
  • Duration: duration (in seconds), UInt32
  • Size: size of the interval.

Return value

  • Array of time frames.

Example

SELECT timeSlots(toDateTime('2012-01-01 12:20:00'), toUInt32(600))
┌─timeSlots(toDateTime('2012-01-01 12:20:00'), toUInt32(600))─┐ 
│ [2012-01-01 12:00:00, 2012-01-01 12:30:00]                  │ 
└─────────────────────────────────────────────────────────────┘

Day of the month
Converts a date or datetime to a UInt8 number representing the day of the month (1-31).
Syntax

toDayOfMonth(date) 
toDayOfMonth(datetime) 
sql

Param

  • date/datetime date/datetime

Return value

  • A day in the month. UInt8

Example

SELECT toDayOfMonth(toDate('2021-08-12')) 
sql
┌─toDayOfMonth(toDate('2021-08-12'))─┐ 
│ 12                                 │ 
└────────────────────────────────────┘
SELECT toDayOfMonth(toDateTime('2021-08-12 00:00:00')) 
sql
┌─toDayOfMonth(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 12                                              │ 
└─────────────────────────────────────────────────┘

time_format

warning

MySQL compatible syntax, recommended to use in relevant dialects.

time_format displays the time time as a string according to the format specified by format. format follows the format in the date_format function.
Syntax

time_format(time, format)

Type

  • time
    • Time to format
    • String
    • Date/Date32/Datetime/Datetime64/Time
  • format
    • Format
    • String

Return type

  • String

Example
Example 1:

SELECT time_format('12:00:00', '%H %k %h %I %l');
┌─TIME_FORMAT('12:00:00', '%H %k %h %I %l')─┐ 
│ 12 12 12 12 12                            │ 
└───────────────────────────────────────────┘

Example 2:

SELECT time_format(datetime '2024-07-21 23:00:00', '%H %k %h %I %l') AS result;
┌─result─────────┐ 
│ 23 23 11 11 11 │ 
└────────────────┘

Example 3:

SELECT time_format(time '23:00:00', '%H %k %h %I %l');
┌─TIME_FORMAT(toTimeType('23:00:00'), '%H %k %h %I %l')─┐ 
│ 23 23 11 11 11                                        │ 
└───────────────────────────────────────────────────────┘

Example 4:

SELECT time_format(date '2024-07-21', '%H %k %h %I %l');
┌─TIME_FORMAT(toDate('2024-07-21'), '%H %k %h %I %l')─┐ 
│ 00 00 12 12 12                                      │ 
└─────────────────────────────────────────────────────┘

time_to_sec

warning

MySQL compatible syntax, recommended to use in relevant dialects.

time_to_sec returns the result of converting time to seconds.
Syntax

time_to_sec(time)

Type

  • time
    • Time to convert to seconds
    • String
    • Date/Date32/Datetime/Datetime64/Time

Return type

  • UInt64

Example
Example 1:

SELECT time_to_sec(datetime '2024-07-21 22:23:00');
┌─time_to_sec(toDateTime('2024-07-21 22:23:00'))─┐ 
│                                          80580 │ 
└────────────────────────────────────────────────┘

Example 2:

sql 
Copy code 
SELECT time_to_sec(date '2024-07-21');
┌─time_to_sec(toDate('2024-07-21'))─┐ 
│                                 0 │ 
└───────────────────────────────────┘

Example 4:

SELECT time_to_sec(time '12:12:12');
┌─time_to_sec(toTimeType('12:12:12'))─┐ 
│                               43932 │ 
└─────────────────────────────────────┘

Example 5:

SELECT time_to_sec('22:23:00');
┌─time_to_sec('22:23:00')─┐ 
│                   80580 │ 
└─────────────────────────┘

toDayOfWeek

warning

MySQL compatible syntax, recommended to use in relevant dialects.

Converts a date or datetime to a UInt8 number representing the day of the week (Monday is 1, Sunday is 7).
Syntax

toDayOfWeek(date) 
toDayOfWeek(datetime)

Param

  • date/datetime date/datetime

Return value

  • Day of the week. UInt8

Example

SELECT toDayOfWeek(toDate('2021-08-12'))
┌─toDayOfWeek(toDate('2021-08-12'))─┐ 
│ 4                                 │ 
└───────────────────────────────────┘
SELECT toDayOfWeek(toDateTime('2021-08-12 00:00:00'))
┌─toDayOfWeek(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 4                                              │ 
└────────────────────────────────────────────────┘

toDayOfYear

warning

MySQL compatible syntax, recommended to use in relevant dialects.

Converts a date or datetime to a UInt16 number representing the total days in the year (1-366).
Syntax

toDayOfYear(date) 
toDayOfYear(datetime)

Param

  • date/datetime date/datetime

Return value

  • A day in a year. UInt8

Example

SELECT toDayOfWeek(toDate('2021-08-12'))
┌─toDayOfWeek(toDate('2021-08-12'))─┐ 
│ 4                                 │ 
└───────────────────────────────────┘
SELECT toDayOfWeek(toDateTime('2021-08-12 00:00:00'))
┌─toDayOfWeek(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 4                                              │ 
└────────────────────────────────────────────────┘

toHour

Convert a Date with Time to a UInt8 number containing the hour in 24-hour format (0-23).
This function assumes that if the clock moves forward, it moves by one hour at 2:00 AM, and if the clock moves backward, it moves by one hour at 3:00 AM (this is not always correct—even in Moscow, the clock was adjusted at different times twice).
Syntax

toHour(datetime)

Param

  • datetime: Date and Time

Return value

  • Hour in 24-hour format. UInt8

Example

SELECT toHour(toDateTime('2021-08-12 00:00:00'))
┌─toHour(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 0                                         │ 
└───────────────────────────────────────────┘

toISOWeek

Convert a Date or Date with Time to a UInt8 number containing the ISO week number.
Syntax

toISOWeek(date) 
toISOWeek(datetime)

Param

  • date/datetimeDate/Date and Time

Return value

  • ISO week number. UInt8

Example

SELECT toISOWeek(toDate('2021-08-12'))
┌─toISOWeek(toDate('2021-08-12'))─┐ 
│ 32                              │ 
└─────────────────────────────────┘
SELECT toISOWeek(toDateTime('2021-08-12 00:00:00'))
┌─toISOWeek(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 32                                           │ 
└──────────────────────────────────────────────┘

toISOYear

Convert a Date or Date with Time to a UInt16 number containing the ISO year number.
Syntax

toISOYear(date) 
toISOYear(datetime)

Param

  • date/datetimeDate/Date and Time

Return value

  • ISO year number. UInt16

Example

SELECT toISOYear(toDate('2021-08-12'))
┌─toISOYear(toDate('2021-08-12'))─┐ 
│ 2021                            │ 
└─────────────────────────────────┘
SELECT toISOYear(toDateTime('2021-08-12 00:00:00'))
┌─toISOYear(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 2021                                         │ 
└──────────────────────────────────────────────┘

toMinute

Convert a Date with Time to a UInt8 number containing the minute(s) of the hour (0-59).
Syntax

toMinute(datetime)

Param

  • datetime: Time

Return value

  • Minute(s) within the hour, Type: UInt8.

Example

SELECT toMinute(toDateTime('2021-08-12 00:00:00'))
┌─toMinute(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 0                                           │ 
└─────────────────────────────────────────────┘

toMonday

Round down a Date or Date and Time to the nearest Monday.
Returns a date.
Syntax

toMonday(date) 
toMonday(datetime)

Param

  • date/datetimeDate/Date and Time

Return value

  • Date to the nearest Monday. Date

Example

SELECT toMonday(toDate('2021-08-12'))
┌─toMonday(toDate('2021-08-12'))─┐ 
│ 2021-08-09                     │ 
└────────────────────────────────┘
SELECT toMonday(toDateTime('2021-08-12 00:00:00'))
┌─toMonday(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 2021-08-09                                  │ 
└─────────────────────────────────────────────┘

toMonth

Convert a Date or Date with Time to a UInt8 number containing the month number (1-12).
Syntax

toMonth(date) 
toMonth(datetime)

Param

  • date/datetimeDate/Date and Time

Return value

  • Month number (1-12). Month.

Example

SELECT toMonday(toDate('2021-08-12'))
┌─toMonday(toDate('2021-08-12'))─┐ 
│ 2021-08-09                     │ 
└────────────────────────────────┘
SELECT toMonday(toDateTime('2021-08-12 00:00:00'))
┌─toMonday(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 2021-08-09                                  │ 
└─────────────────────────────────────────────┘

toQuarter

Convert a Date or Date with Time to a UInt8 number containing the quarter number.
Syntax

toQuarter(date) 
toQuarter(datetime)

Param

  • date/datetimeDate/Date and Time

Return value

  • Quarter number. UInt8

Example

SELECT toQuarter(toDate('2021-08-12'))
┌─toQuarter(toDate('2021-08-12'))─┐ 
│ 3                               │ 
└─────────────────────────────────┘
SELECT toQuarter(toDateTime('2021-08-12 00:00:00'))
┌─toQuarter(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 3                                            │ 
└──────────────────────────────────────────────┘

toRelativeDayNum

Convert a Date with Time or Date to the number of days since a fixed point in the past.
Syntax

toRelativeDayNum(date) 
toRelativeDayNum(datetime)

Param

  • date/datetimeDate/Date and Time

Return value

  • Number of days since a fixed point in the past. UInt16

Example

SELECT toRelativeDayNum(toDate('2021-08-12'))
┌─toRelativeDayNum(toDate('2021-08-12'))─┐ 
│ 18851                                  │ 
└────────────────────────────────────────┘
SELECT toRelativeDayNum(toDateTime('2021-08-12 00:00:00'))
┌─toRelativeDayNum(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 18851                                               │ 
└─────────────────────────────────────────────────────┘

toRelativeHourNum

Convert a Date with Time or Date to the number of hours since a fixed point in the past.
Syntax

toRelativeHourNum(date) 
toRelativeHourNum(datetime)

Param

  • date/datetimeDate/Date and Time

Return value

  • Number of hours since a fixed point in the past. UInt32

Example

SELECT toRelativeHourNum(toDate('2021-08-12'))
┌─toRelativeHourNum(toDate('2021-08-12'))─┐ 
│ 452416                                  │ 
└─────────────────────────────────────────┘
SELECT toRelativeHourNum(toDateTime('2021-08-12 00:00:00'))
┌─toRelativeHourNum(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 452416                                               │ 
└──────────────────────────────────────────────────────┘

toRelativeMinuteNum

Convert a date or datetime to the number of minutes since a fixed point in the past.
Syntax

toRelativeMinuteNum(date) 
toRelativeMinuteNum(datetime)

Param

  • date/datetimeDate/DateTime

Return value

  • Number of minutes since a fixed point in the past. UInt32

Example

SELECT toRelativeMinuteNum(toDate('2021-08-12'))
┌─toRelativeMinuteNum(toDate('2021-08-12'))─┐ 
│ 27144960                                  │ 
└───────────────────────────────────────────┘
SELECT toRelativeMinuteNum(toDateTime('2021-08-12 00:00:00'))
┌─toRelativeMinuteNum(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 27144960                                               │ 
└────────────────────────────────────────────────────────┘

toRelativeMonthNum

Convert a date or datetime to the number of months since a fixed point in the past.
Syntax

toRelativeMonthNum(date) 
toRelativeMonthNum(datetime)

Param

  • date/datetimeDate/DateTime

Return value

  • Number of months since a fixed point in the past. UInt16

Example

SELECT toRelativeMonthNum(toDate('2021-08-12'))
┌─toRelativeMonthNum(toDate('2021-08-12'))─┐ 
│ 24260                                    │ 
└──────────────────────────────────────────┘
SELECT toRelativeMonthNum(toDateTime('2021-08-12 00:00:00'))
┌─toRelativeMonthNum(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 24260                                                 │ 
└───────────────────────────────────────────────────────┘

toRelativeQuarterNum

Convert a date or datetime to the number of quarters since a fixed point in the past.
Syntax

toRelativeQuarterNum(date) 
toRelativeQuarterNum(datetime)

Param

  • date/datetimeDate/DateTime

Return value

  • Number of quarters since a fixed point in the past. UInt16

Example

SELECT toRelativeQuarterNum(toDate('2021-08-12'))
┌─toRelativeQuarterNum(toDate('2021-08-12'))─┐ 
│ 8086                                       │ 
└────────────────────────────────────────────┘
SELECT toRelativeQuarterNum(toDateTime('2021-08-12 00:00:00'))
┌─toRelativeQuarterNum(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 8086                                                    │ 
└─────────────────────────────────────────────────────────┘

toRelativeSecondNum

Convert a date or datetime to the number of seconds since a fixed point in the past.
Syntax

toRelativeSecondNum(date) 
toRelativeSecondNum(datetime)

Param

  • date/datetimeDate/DateTime

Return value

  • Number of seconds since a fixed point in the past. UInt32

Example

SELECT toRelativeSecondNum(toDate('2021-08-12'))
┌─toRelativeSecondNum(toDate('2021-08-12'))─┐ 
│ 1628697600                                │ 
└───────────────────────────────────────────┘
SELECT toRelativeSecondNum(toDateTime('2021-08-12 00:00:00'))
┌─toRelativeSecondNum(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 1628697600                                             │ 
└────────────────────────────────────────────────────────┘

toRelativeWeekNum

Convert a date or datetime to the number of weeks since a fixed point in the past.
Syntax

toRelativeWeekNum(date) 
toRelativeWeekNum(datetime)

Param

  • date/datetimeDate/DateTime

Return value

  • Number of weeks since a fixed point in the past. UInt16

Example

SELECT toRelativeWeekNum(toDate('2021-08-12'))
┌─toRelativeWeekNum(toDate('2021-08-12'))─┐ 
│ 2693                                    │ 
└─────────────────────────────────────────┘
SELECT toRelativeWeekNum(toDateTime('2021-08-12 00:00:00'))
┌─toRelativeWeekNum(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 2693                                                 │ 
└──────────────────────────────────────────────────────┘

toRelativeYearNum

Convert a date or datetime to the number of years since a fixed point in the past.
Syntax

toRelativeYearNum(date) 
toRelativeYearNum(datetime)

Param

  • date/datetimeDate/DateTime

Return value

  • Number of years since a fixed point in the past. UInt16

Example

SELECT toRelativeYearNum(toDate('2021-08-12'))
┌─toRelativeYearNum(toDate('2021-08-12'))─┐ 
│ 2021                                    │ 
└─────────────────────────────────────────┘
SELECT toRelativeYearNum(toDateTime('2021-08-12 00:00:00'))
┌─toRelativeYearNum(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 2021                                                 │ 
└──────────────────────────────────────────────────────┘

toSecond

Convert a date with time to a UInt8 number (0-59) representing the number of seconds in a minute.
Leap seconds are not considered.
Syntax

toSecond(datetime)

Param

  • datetime: DateTime

Return value

  • Number of seconds in a minute. Type: UInt8.

Example

SELECT toSecond(toDateTime('2021-08-12 00:00:00'))
┌─toSecond(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 0                                           │ 
└─────────────────────────────────────────────┘

toStartOfDay

Round datetime down to the start time of the day.
Syntax

toStartOfDay(datetime)

Param

  • datetime: DateTime

Return value

  • Round datetime down to the start time of the day. DateTime

Example

SELECT toStartOfDay(toDateTime('2021-08-12 08:00:00'))
┌─toStartOfDay(toDateTime('2021-08-12 08:00:00'))─┐ 
│ 2021-08-12 00:00:00                             │ 
└─────────────────────────────────────────────────┘

toStartOfFifteenMinutes

Round datetime down to the start time of the fifteen-minute intervals.
Syntax

toStartOfFifteenMinutes(datetime)

Param

  • datetime: DateTime

Return value

  • Rounds down a DateTime value to the start time of a fifteen-minute interval. Type: DateTime.

Example

SELECT toStartOfFifteenMinutes(toDateTime('2021-08-12 00:10:00'))
┌─toStartOfFifteenMinutes(toDateTime('2021-08-12 00:10:00'))─┐ 
│ 2021-08-12 00:00:00                                        │ 
└────────────────────────────────────────────────────────────┘

toStartOfFiveMinute

Rounds down a DateTime value to the start time of a five-minute interval.
Syntax

toStartOfFiveMinute(datetime)

Param

  • datetim: DateTime

Return value

  • Rounds down a DateTime value to the start time of a five-minute interval. Type: DateTime.

Example

SELECT toStartOfFiveMinute(toDateTime('2021-08-12 00:09:00'))
┌─toStartOfFiveMinute(toDateTime('2021-08-12 00:09:00'))─┐ 
│ 2021-08-12 00:05:00                                    │ 
└────────────────────────────────────────────────────────┘

toStartOfHour

Rounds down a DateTime value to the start of the hour.
Syntax

toStartOfHour(datetime)

Param

  • datetimeDateTime

Return value

  • Rounds down a DateTime value to the start of the hour. DateTime

Example

SELECT toStartOfHour(toDateTime('2021-08-12 01:09:00'))
┌─toStartOfHour(toDateTime('2021-08-12 01:09:00'))─┐ 
│ 2021-08-12 01:00:00                              │ 
└──────────────────────────────────────────────────┘

toStartOfISOYear

Rounds down a date or datetime value to the first day of the ISO year.
Returns a date.
Syntax

toStartOfISOYear(date) 
toStartOfISOYear(datetime)

Param

  • date/datetimeDate/dateTime

Return value

  • Rounds down a date or datetime value to the first day of the ISO year. Date

Example

SELECT toStartOfISOYear(toDate('2021-08-12'))
┌─toStartOfISOYear(toDate('2021-08-12'))─┐ 
│ 2021-01-04                             │ 
└────────────────────────────────────────┘
SELECT toStartOfISOYear(toDateTime('2021-08-12 00:00:00'))
┌─toStartOfISOYear(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 2021-01-04                                          │ 
└─────────────────────────────────────────────────────┘

toStartOfInterval

This is a generalization of other functions named toStartOf*. For example,
toStartOfInterval(t, INTERVAL 1 year) returns the same value as toStartOfYear(t),
toStartOfInterval(t, INTERVAL 1 month) returns the same value as toStartOfMonth(t),
toStartOfInterval(t, INTERVAL 1 day) returns the same value as toStartOfDay(t),
toStartOfInterval(t, INTERVAL 15 minute) returns the same value as toStartOfFifteenMinutes(t), and so on.
Syntax

toStartOfInterval(time_or_data, INTERVAL x unit [, time_zone])

Param

  • time_or_dataDate/DateTime
  • x unitx is UInt*, the unit can be seconds, minutes, hours, day, month, year, or quarters

Return value

  • Date/DateTime

Example

SELECT toStartOfInterval(toDateTime('2021-08-12 01:09:00'), INTERVAL 2 minute)
┌─toStartOfInterval(toDateTime('2021-08-12 01:09:00'), toIntervalMinute(2))─┐ 
│ 2021-08-12 01:08:00                                                       │ 
└───────────────────────────────────────────────────────────────────────────┘

toStartOfMinute

Rounds down a DateTime value to the start of the minute.
Syntax

toStartOfMinute(datetime)

Param

  • datetimeDateTime

Return value

  • Rounds down a DateTime value to the start of the minute. DateTime

Example

SELECT toStartOfMinute(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfMinute(toDateTime('2021-08-12 00:01:30'))─┐ 
│ 2021-08-12 00:01:00                                │ 
└────────────────────────────────────────────────────┘

toStartOfMonth

Rounds down a date or datetime value to the first day of the month.
Returns a date.
Syntax

toStartOfMonth(date) 
toStartOfMonth(datetime) 
sql

Param

  • date/datetimeDate/DateTime

Return value

  • Rounds down a date or datetime value to the first day of the month. Date

Example

SELECT toStartOfMonth(toDate('2021-08-12'))
┌─toStartOfMonth(toDate('2021-08-12'))─┐ 
│ 2021-08-01                           │ 
└──────────────────────────────────────┘
SELECT toStartOfMonth(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfMonth(toDateTime('2021-08-12 00:01:30'))─┐ 
│ 2021-08-01                                        │ 
└───────────────────────────────────────────────────┘

toStartOfQuarter

Rounds down a date or datetime value to the first day of the quarter.
The first day of a quarter can be January 1, April 1, July 1, or October 1.
Returns a date.
Syntax

toStartOfQuarter(date) 
toStartOfQuarter(datetime)

Param

  • date/datetimeDate/DateTime

Return value

  • Rounds down a date or datetime value to the first day of the quarter. Date

Example

SELECT toStartOfQuarter(toDate('2021-08-12'))
┌─toStartOfQuarter(toDate('2021-08-12'))─┐ 
│ 2021-07-01                             │ 
└────────────────────────────────────────┘
SELECT toStartOfQuarter(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfQuarter(toDateTime('2021-08-12 00:01:30'))─┐ 
│ 2021-07-01                                          │ 
└─────────────────────────────────────────────────────┘

toStartOfTenMinutes

Rounds down a DateTime value to the start time of a ten-minute interval.
Syntax

toStartOfTenMinutes(datetime)

Param

  • datetimeDateTime

Return value

  • Rounds down a DateTime value to the start time of a ten-minute interval. DateTime

Example

SELECT toStartOfTenMinutes(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfTenMinutes(toDateTime('2021-08-12 00:01:30'))─┐ 
│ 2021-08-12 00:00:00                                    │ 
└────────────────────────────────────────────────────────┘

toStartOfWeek

Round down the date or datetime to the nearest Sunday or Monday according to the mode.
Returns the date. mode
The function of the mode parameter is exactly the same as the mode parameter in toWeek(). For single-parameter syntax, use mode value 0.
Syntax

toStartOfWeek(date[,mode]) 
toStartOfWeek(datetime[,mode])

Parameters

  • date/datetimeDate/Datetime
  • modeRefer to week mode.

Return value

  • Round down the date or datetime to the nearest Sunday or Monday according to the mode.

Example

SELECT toStartOfWeek(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfWeek(toDateTime('2021-08-12 00:01:30'))─┐ 
│ 2021-08-08                                       │ 
└──────────────────────────────────────────────────┘

toStartOfYear

Round down the date or datetime to the first day of the year.
Returns the date.
Syntax

toStartOfYear(date) 
toStartOfYear(datetime)

Parameters

  • date/datetimeDate/Datetime

Return value

  • Round down the date or datetime to the first day of the year. Date

Example

SELECT toStartOfYear(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfYear(toDateTime('2021-08-12 00:01:30'))─┐ 
│ 2021-01-01                                       │ 
└──────────────────────────────────────────────────┘

toTime

Convert a datetime to a fixed date while retaining the time.
Syntax

toTime(date) 
toTime(datetime)

Parameters

  • date/datetime: Date/Datetime

Return value

  • Date and time

Example

SELECT toTime(toDateTime('2021-08-12 00:01:30'))
┌─toTypeName(toTime(toDateTime('2021-08-12 00:01:30')))─┐ 
│ DateTime('PRC')                                       │ 
└───────────────────────────────────────────────────────┘

toTimezone

Convert time or datetime to the specified timezone. Timezone is a property of the Date and DateTime data types. The internal value (seconds) of a table field or result set column remains unchanged, but the column's type changes, and its string representation changes accordingly.
Syntax

toTimezone(value, timezone)

Parameters

  • value— Time or datetime. Date/Datetime.
  • timezone— Timezone of the return value. String.

Return 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;
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

toTimeZone(time_utc, 'Asia/Yekaterinburg')Changes the type from DateTime('UTC') to DateTime('Asia/Yekaterinburg'). The value (Unix timestamp) 1546300800 remains unchanged, but the string representation (the result of the toString() function) changes from time_utc: 2019-01-01 00:00:00 to time_yekat: 2019-01-01 05:00:00.

toWeek

This function returns the number of weeks for a date or datetime. The two-parameter form of toWeek() allows you to specify whether the week starts on Sunday or Monday, and whether the return value should be in the range 0 to 53 or 1 to 53. If the mode parameter is omitted, the default mode is 0. toISOWeek() is a compatible function equivalent to toWeek(date,3).
The following table describes how the mode parameter works.

| | | | | \

ModeThe first day of the weekRangeThe first week is the first week ...
\
0Sunday0-53The year has a Sunday
\
1Monday0-53The year has 4 days or more
\
2Sunday1-53The year has a Sunday
\
3Monday1-53The year has 4 days or more
\
4Sunday0-53The year has 4 days or more
\
5Monday0-53This year has one Monday
\
6Sunday1-53This year has 4 days or more
\
7Monday1-53This year has one Monday
\
8Sunday1-53Contains January 1
\
9Monday1-53Contains January 1

For mode values meaning "This year has 4 days or more", the number of weeks is numbered according to ISO 8601:1988:

  • If the week containing January 1 has 4 days or more in the new year, then that week is week 1.
  • Otherwise, it is the last week of the previous year, and the next week is week 1.

For mode values meaning "contains January 1", the week containing January 1 is week 1. It does not matter how many days of the new year are included in that week, even if only one day is included.
Syntax

toWeek(date, [, mode][, Timezone])

Parameters

  • date – Date or DateTime.
  • mode – Optional parameter, value range is [0,9], default is 0.
  • Timezone – Optional parameter, its behavior is the same as any other conversion function.

Return value

  • Number of weeks. UInt8

Example

SELECT toDate('2016-12-27') AS date, toWeek(date) AS week0, toWeek(date,1) AS week1, toWeek(date,9) AS week9;
┌─date───────┬─week0─┬─week1─┬─week9─┐ 
│ 2016-12-27 │ 52    │ 52    │ 1     │ 
└────────────┴───────┴───────┴───────┘

toYYYYMM

Convert date or datetime to a UInt32 number containing year and month (YYYY * 100 + MM).
Syntax

toYYYYMM(date) 
toYYYYMM(datetime)

Parameters

  • date/datetime Date/DateTime

Return value

  • Number containing year and month. UInt32

Example

SELECT toYYYYMM(toDate('2021-08-12'))
┌─toYYYYMM(toDate('2021-08-12'))─┐ 
│ 202108                         │ 
└────────────────────────────────┘
SELECT toYYYYMM(toDateTime('2021-08-12 00:00:00'))
┌─toYYYYMM(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 202108                                      │ 
└─────────────────────────────────────────────┘

toYYYYMMDD

Convert date or datetime to a UInt32 number containing year, month, and day (YYYY * 10000 + MM * 100 + DD).
Syntax

toYYYYMMDD(date) 
toYYYYMMDD(datetime)

Parameters

  • date/datetime Date/DateTime

Return value

  • Number containing year, month, and day. UInt32

Example

SELECT toYYYYMMDD(toDate('2021-08-12'))
┌─toYYYYMMDD(toDate('2021-08-12'))─┐ 
│ 20210812                         │ 
└──────────────────────────────────┘
SELECT toYYYYMMDD(toDateTime('2021-08-12 00:00:00'))
┌─toYYYYMMDD(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 20210812                                      │ 
└───────────────────────────────────────────────┘

toYYYYMMDDhhmmss

Convert date or datetime with time to a UInt64 number containing year, month, day, hour, minute, and seconds (YYYY * 10000000000 + MM * 100000000 + DD * 1000000 + hh * 10000 + mm * 100 + ss).
Syntax

toYYYYMMDDhhmmss(date) 
toYYYYMMDDhhmmss(datetime)

Parameters

  • date/datetime Date/DateTime

Return value

  • Number containing year, month, day, hour, minute, and seconds. UInt32

Example

SELECT toYYYYMMDDhhmmss(toDate('2021-08-12'))
┌─toYYYYMMDDhhmmss(toDate('2021-08-12'))─┐ 
│ 20210812000000                         │ 
└────────────────────────────────────────┘
SELECT toYYYYMMDDhhmmss(toDateTime('2021-08-12 00:00:00'))
┌─toYYYYMMDDhhmmss(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 20210812000000                                      │ 
└─────────────────────────────────────────────────────┘

toYear

Convert date or datetime to a UInt16 number containing the year (AD).
Syntax

toYear(date) 
toYear(datetime)

Parameters

  • date/datetime Date/DateTime

Return value

  • Convert date or datetime to a UInt16 number containing the year (AD). UInt16

Example

SELECT toYear(toDate('2021-08-12'))
┌─toYear(toDate('2021-08-12'))─┐ 
│ 2021                         │ 
└──────────────────────────────┘
SELECT toYear(toDateTime('2021-08-12 00:00:00'))
┌─toYear(toDateTime('2021-08-12 00:00:00'))─┐ 
│ 2021                                      │ 
└───────────────────────────────────────────┘

toYearWeek

Return the year and week of the date. The year in the result may differ from the year of the first and last week in the date parameter.
The function of the mode parameter is exactly the same as the mode parameter in toWeek(). For single-parameter syntax, use mode value 0.
toISOYear() is a compatible function equivalent to intDiv(toYearWeek(date,3),100).
Syntax

toYearWeek(date[,mode]) 
toYearWeek(datetime[,mode])

Parameters

  • date/datetime Date/DateTime
  • mode Refer to week mode.

Return value

  • Year and week of the date. UInt32

Example

SELECT toDate('2016-12-27') AS date, toYearWeek(date) AS yearWeek0, toYearWeek(date,1) AS yearWeek1, toYearWeek(date,9) AS yearWeek9;
┌─date───────┬─yearWeek0─┬─yearWeek1─┬─yearWeek9─┐ 
│ 2016-12-27 │ 201652    │ 201652    │ 201701    │ 
└────────────┴───────────┴───────────┴───────────┘

today

Accepts zero parameters and returns the current date at the moment the request is executed.
Equivalent to 'toDate(now())'.
Syntax

today()

Return value

  • Date.

Example

SELECT today()

to_days

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

to_days returns the number of days since year 0 AD based on the specified date.
Syntax

to_days(date)

Type

  • date
    • The date to be converted to the number of days
    • Date/Date32/Datetime/Datetime64
    • String

Return type

  • UInt64

Example
Example 1:

SELECT to_days(date '2024-07-21');
┌─to_days(toDate('2024-07-21'))─┐ 
│                        739453 │ 
└───────────────────────────────┘

Example 2:

SELECT to_days('2024-07-21');
┌─to_days('2024-07-21')─┐ 
│                739453 │ 
└───────────────────────┘

to_seconds

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

to_seconds returns the number of seconds since year 0 AD based on the given expr.
Syntax

to_seconds(expr)

Type

  • expr
    • The expression to be converted to seconds
    • Date/Date32/Datetime/Datetime64
    • String
    • Time

Return type

  • UInt64

Example
Example 1:

SELECT to_seconds(date '2024-07-21');
┌─to_seconds(toDate('2024-07-21'))─┐ 
│                      63888710400 │ 
└──────────────────────────────────┘

Example 2:

SELECT to_seconds(datetime '2024-07-21 12:13:14');
┌─to_seconds(toDateTime('2024-07-21 12:13:14'))─┐ 
│                                   63888754394 │ 
└───────────────────────────────────────────────┘

unix_timestamp

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

unix_timestamp returns the number of seconds from '1970-01-01 00:00:00' UTC to the current time. unix_timestamp(date) returns the value of the parameter as the number of seconds from '1970-01-01 00:00:00' UTC to the specified date.
Syntax

unix_timestamp() 
unix_timestamp(date)

Type

  • date
    • The date or datetime expression to be converted to a UNIX timestamp
    • String
    • Date/Date32/Datetime/Datetime64

Return type

  • Int64

Example
Example 1:

SELECT unix_timestamp();
┌─toUnixTimestamp()─┐ 
│        1721987167 │ 
└───────────────────┘

Example 2:

SELECT unix_timestamp(datetime '2024-07-21 12:13:14');
┌─toUnixTimestamp(toDateTime('2024-07-21 12:13:14'))─┐ 
│                                         1721535194 │ 
└────────────────────────────────────────────────────┘

Example 3:

SELECT unix_timestamp(date '2024-07-21');
┌─toUnixTimestamp(toDate('2024-07-21'))─┐ 
│                            1721491200 │ 
└───────────────────────────────────────┘

utc_date

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

utc_date returns the UTC date.
Syntax

utc_date()

Return type

  • Date

Example

SELECT utc_date();
┌─utc_date()─┐ 
│ 2024-07-26 │ 
└────────────┘

utc_time

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

utc_time returns the UTC time.
Syntax

utc_time()

Return type

  • Time

Example

SELECT utc_time();
┌───utc_time()─┐ 
│ 09:49:58.000 │ 
└─────────────┘

utc_timestamp

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

utc_timestamp returns the UTC timestamp.
Syntax

utc_timestamp()

Return type

  • Datetime

Example

SELECT utc_timestamp();
┌──────UTCTimestamp()─┐ 
│ 2024-07-26 09:50:19 │ 
└─────────────────────┘

week

Alias of ToWeek

weekday

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

weekday returns which day of the week date is. Return value: 0 = Monday, 1 = Tuesday, 2 = Wednesday, 3 = Thursday, 4 = Friday, 5 = Saturday, 6 = Sunday.
Syntax

weekday(date)

Type

  • date
    • Date/Date32/Datetime/Datetime64
    • String

Return type

  • UInt8

Example
Example 1:

SELECT weekday(datetime '2024-07-21 00:09:00');
┌─WEEKDAY(toDateTime('2024-07-21 00:09:00'))─┐ 
│                                          6 │ 
└────────────────────────────────────────────┘

Example 2:

SELECT weekday(date '2024-07-21');
┌─WEEKDAY(toDate('2024-07-21'))─┐ 
│                             6 │ 
└───────────────────────────────┘

Example 3:

SELECT weekday('2024-07-21');
┌─WEEKDAY('2024-07-21')─┐ 
│                     6 │ 
└───────────────────────┘

weekofyear

warning

MySQL-compatible syntax, recommended for use in relevant dialects.

weekofyear returns the calendar week corresponding to date, with a value range of [1, 53].
Syntax

weekofyear(date)

Type

  • date
    • Date/Date32/Datetime/Datetime64
    • String

Return type

  • UInt64

Example
Example 1:

SELECT weekofyear(datetime '2024-07-21 09:00:00');
┌─toWeekOfYear(toDateTime('2024-07-21 09:00:00'))─┐ 
│                                              29 │ 
└─────────────────────────────────────────────────┘

Example 2:

SELECT weekofyear(date '2024-07-21');
┌─toWeekOfYear(toDate('2024-07-21'))─┐ 
│                                 29 │ 
└────────────────────────────────────┘

Example 3:

SELECT weekofyear('2024-07-21');
┌─toWeekOfYear('2024-07-21')─┐ 
│                         29 │ 
└────────────────────────────┘

year

Alias of toYear

yearweek

Returns the year and week of the date. The year in the result may differ from the year of the first and last week of the year in the date parameter.
The mode parameter functions exactly the same as the mode parameter in toWeek(). For single-parameter syntax, use mode value 0.
toISOYear() is a compatible function equivalent to intDiv(toYearWeek(date,3),100).
Syntax

toYearWeek(date[,mode])

Param

  • date/datetime Date/datetime

Return value

  • Round down the Date or date with Time to the first day of the ISO year. Date

Example

SELECT toDate('2016-12-27') AS date, toYearWeek(date) AS yearWeek0, toYearWeek(date,1) AS yearWeek1, toYearWeek(date,9) AS yearWeek9;
┌─date───────┬─yearWeek0─┬─yearWeek1─┬─yearWeek9─┐ 
│ 2016-12-27 │ 201652    │ 201652    │ 201701    │ 
└────────────┴───────────┴───────────┴───────────┘

yesterday

Accepts Zero Param and returns yesterday's Date at a certain moment during the execution of the request.
Same as 'today() - 1'.
Syntax

yesterday()

Return value

  • Date.

Example

SELECT yesterday()

Last updated: 2026.03.31 16:13:52