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
dateINTERVAL expr unitexprdaysReturn 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 │ └─────────────────────────────────────┘
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/DateTimeinterval Date/DateTimeReturn value
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 │ └────────────────────┴─────────────────────────┘
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/DateTimeinterval Date/DateTimeReturn value
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 │ └─────────────────────┴──────────────────────────┘
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/DateTimeinterval Date/DateTimeReturn value
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 │ └───────────────────────┴────────────────────────────┘
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/DateTimeinterval Date/DateTimeReturn value
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 │ └──────────────────────┴───────────────────────────┘
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/DateTimeinterval Date/DateTimeReturn value
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 │ └────────────────────────┴─────────────────────────────┘
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/DateTimeinterval Date/DateTimeReturn value
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 │ └───────────────────────┴────────────────────────────┘
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 valueinterval Date or date-time intervalReturn 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 │ └─────────────────────┴──────────────────────────┘
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 valueinterval Date or date-time intervalReturn 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 │ └─────────────────────┴──────────────────────────┘
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
expr2 Incremental time
Return type
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 │ └────────────────────────────────────────────┘
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
exprfrom_tzto_tzReturn type
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 │ └─────────────────────────────────────────────────┘
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
Example
SELECT curdate();
┌────today()─┐ │ 2024-07-25 │ └────────────┘
warning
MySQL compatible syntax. It is recommended to use in relevant dialects.
curtime function returns the current time.
Syntax
curtime()
Return type
Example
SELECT curtime();
┌────CURTIME()─┐ │ 16:39:50.844 │ └──────────────┘
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
exprReturn type
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 │ └─────────────────────────────┘
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:
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 │ └────────────────────────────────────────────────────────────────────────────────────────┘
warning
MySQL compatible syntax. It is recommended to use in relevant dialects.
Same as formatDateTime()
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 │ └───────────────────────────────────────────────────────┘
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 │ └───────────────────────────────────────────────────────┘
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:
| | | \
| Unit | Compatibility | |
|---|---|---|
| \ | ||
nanosecond | Compatible only with DateTime64 | |
| \ | ||
microsecond | Compatible only with DateTime64 | |
| \ | ||
millisecond | Compatible only with DateTime64 | |
| \ | ||
second | Compatible with DateTime64, DateTime | |
| \ | ||
minute | Compatible with DateTime64, DateTime | |
| \ | ||
hour | Compatible with DateTime64, DateTime | |
| \ | ||
day | Compatible with DateTime64, DateTime, Date | |
| \ | ||
week | Compatible with DateTime64, DateTime, Date | |
| \ | ||
month | Compatible with DateTime64, DateTime, Date | |
| \ | ||
quarter | Compatible with DateTime64, DateTime, Date | |
| \ | ||
year | Compatible 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 parameter | datetime parameter | Return type | |
|---|---|---|---|
| \ | |||
| year, quarter, month, week | Date32 or DateTime64 or Date or DateTime | Date32 or Date | |
| \ | |||
| day, hour, minute, second | Date32, DateTime64, Date or DateTime | DateTime64 or DateTime | |
| \ | |||
| millisecond, microsecond | Any | DateTime64 | |
| \ | |||
| nanosecond | None | Precision 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 │ └─────────────────────┴────────────────────────────────────────────┘
Returns the day of the month from the date.
Syntax
DAY(expr) DAYOFMONTH(expr)
Parameter type
exprReturn type
UInt8Example
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 │ └───────────────────┘
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
exprReturn type
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 │ └────────────────────────────────┘
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
exprReturn type
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
exprReturn type
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 │ └─────────────────────────┘
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
unitunit can be: second, minute, hour, day, month, year, minute_second, hour_second, hour_minute, day_second, day_minute, day_hour, year_month.dateReturn type
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 │ └────────┘
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
exprReturn type
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
exprformatReturn type
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 │ └───────────────────────────┘
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/dateTimeFormat format patternReplacement 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.
| | | | \
| Placeholder | Description | Example | |
|---|---|---|---|
| \ | |||
| %C | year divided by 100 and truncated to integer (00-99) | \ | |
| Year divided by 100 and truncated to integer (00-99) | 20 | ||
| \ | |||
| %d | day of the month, zero-padded (01-31) | \ | |
| Day of the month, zero-padded (01-31) | 02 | ||
| \ | |||
| %D | Short MM/DD/YY date, equivalent to %m/%d/%y | \ | |
| Short MM/DD/YY date, equivalent to %m/%d/%y | 01/02/18 | ||
| \ | |||
| %e | day of the month, space-padded ( 1-31) | \ | |
| Day of the month, space-padded (1-31) | 2 | ||
| \ | |||
| %F | short YYYY-MM-DD date, equivalent to %Y-%m-%d | \ | |
| Short YYYY-MM-DD date, equivalent to %Y-%m-%d | 2018-01-02 | ||
| \ | |||
| %H | hour in 24h format (00-23) | \ | |
| Hour in 24h format (00-23) | 22 | ||
| \ | |||
| %I | hour in 12h format (01-12) | \ | |
| Hour in 12h format (01-12) | 10 | ||
| \ | |||
| %j | day of the year (001-366) | \ | |
| Day of the year (001-366) | 002 | ||
| \ | |||
| %m | month as a decimal number (01-12) | \ | |
| Month as a decimal number (01-12) | 01 | ||
| \ | |||
| %M | minute (00-59) | \ | |
| Minute (00-59) | 33 | ||
| \ | |||
| %n | new-line character (‘’) | \ | |
| New-line character ('') | |||
| \ | |||
| %p | AM or PM designation | \ | |
| AM or PM designation | PM | ||
| \ | |||
| %R | 24-hour HH:MMtime, equivalent to %H:%M | \ | |
| 24-hour HH:MM time, equivalent to %H:%M | 22:33 | ||
| \ | |||
| %S | second (00-59) | \ | |
| Second (00-59) | 44 | \ | |
| \ | |||
| %t | \ | ||
| horizontal-tab character (’) | \ | ||
| Horizontal-tab character ('') | |||
| \ | |||
| %T | ISO8601 time format (HH:MM:SS), equivalent to %H:%M:%S | \ | |
| ISO8601 Time format (HH:MM:SS), equivalent to %H:%M:%S | 22:33:44 | ||
| \ | |||
| %u | ISO8601 weekday as number with Monday as 1 (1-7) | \ | |
| ISO8601 weekday as a number, Monday is 1 (1-7) | 2 | ||
| \ | |||
| %V | ISO8601 week number (01-53) | \ | |
| ISO8601 number of weeks (01-53) | 01 | ||
| \ | |||
| %w | weekday as a decimal number with Sunday as 0 (0-6) | \ | |
| Weekday represented as a decimal number, Sunday is 0 (0-6) | 2 | ||
| \ | |||
| %y | Year, last two digits (00-99) | \ | |
| Year, last two digits (00-99) | 18 | ||
| \ | |||
| %Y | Year | \ | |
| year | 2018 | ||
| \ | |||
| %% | 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 │ └─────────────────────┘
Returns the hour in the Date Time, same as toHour().
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
exprReturn Type
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 │ └─────────────────────────────────────────┘
Returns the current Date and Time, same as now().
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
yeardayofyearReturn Type
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 │ └─────────────────────────┴─────────────────────────┘
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
hourminutesecondReturn Type
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 │ └────────────────────────────┘
Returns the minute in the Date Time, same as toMinute().
Returns the month in the Date Time, same as toMonth().
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
dateReturn Type
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 │ └─────────────────────────┘
Returns the current Date and Time.
Syntax
now()
Return value
Type: Date Time.
Example
SELECT now();
┌─now()───────────────┐ │ 2021-08-11 14:35:47 │ └─────────────────────┘
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
expr1expr2Return Type
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 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
expr1expr2Return Type
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 │ └─────────────────────────────────┘
Returns the quarter in the Date Time, same as toQuarter().
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
secondsReturn type
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 │ └─────────────────────┘
Returns the seconds in the DateTime, same as toSecond().
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
strformatReturn type
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 │ └────────────────────────────┘
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/DateTimeintervalDate/DateTimeReturn value
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 │ └─────────────────────────┴──────────────────────────────┘
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/DateTimeintervalDate/DateTimeReturn value
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 │ └──────────────────────────┴───────────────────────────────┘
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/DateTimeintervalDate/DateTimeReturn value
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 │ └────────────────────────────┴─────────────────────────────────┘
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/DateTimeintervalDate/DateTimeReturn value
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 │ └───────────────────────────┴────────────────────────────────┘
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/DateTimeintervalDate/DateTimeReturn value
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 │ └─────────────────────────────┴──────────────────────────────────┘
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/DateTimeintervalDate/DateTimeReturn value
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 │ └────────────────────────────┴─────────────────────────────────┘
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/DateTimeintervalDate/DateTimeReturn value
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 │ └──────────────────────────┴───────────────────────────────┘
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/DateTimeintervalDate/DateTimeReturn value
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 │ └──────────────────────────┴───────────────────────────────┘
warning
MySQL-compatible syntax, Recommended for use in relevant dialects.
subtime Returns the time after expr1 minus expr2.
Syntax
subtime(expr1, expr2)
Type
expr1expr2Return type
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 │ └─────────────────────────────────────────┘
Returns the current system time, same as now().
warning
MySQL-compatible syntax, Recommended for use in relevant dialects.
Returns the time in expr.
Syntax
time(expr[, scale])
Type
exprscaleReturn type
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 │ └─────────────────────────────────────────────────────┘
warning
MySQL-compatible syntax, Recommended for use in relevant dialects.
Returns the time difference between two inputs, same as subtime().
warning
MySQL-compatible syntax, Recommended for use in relevant dialects.
timestamp Returns the date or DateTime represented by expr.
Syntax
timestamp(expr)
Type
exprReturn type
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 │ └────────────────────────────┘
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
unitintervaldatetime_exprReturn type
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 │ └─────────────────────┘
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
unitdatetime_expr1datetime_expr2Return type
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 │ └───────────────────────────────────────────────────────────────────────┘
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
datetimeReturn value
DateTimeExample
SELECT timeSlot(toDateTime('2018-01-01 01:01:01'))
┌─timeSlot(toDateTime('2018-01-01 01:01:01'))─┐ │ 2018-01-01 01:00:00 │ └─────────────────────────────────────────────┘
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 timeDuration: duration (in seconds), UInt32Size: size of the interval.Return value
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/datetimeReturn value
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 │ └─────────────────────────────────────────────────┘
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
timeformatReturn type
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 │ └─────────────────────────────────────────────────────┘
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
timeReturn type
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 │ └─────────────────────────┘
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/datetimeReturn value
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 │ └────────────────────────────────────────────────┘
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/datetimeReturn value
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 │ └────────────────────────────────────────────────┘
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 TimeReturn value
Example
SELECT toHour(toDateTime('2021-08-12 00:00:00'))
┌─toHour(toDateTime('2021-08-12 00:00:00'))─┐ │ 0 │ └───────────────────────────────────────────┘
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 TimeReturn value
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 │ └──────────────────────────────────────────────┘
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 TimeReturn value
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 │ └──────────────────────────────────────────────┘
Convert a Date with Time to a UInt8 number containing the minute(s) of the hour (0-59).
Syntax
toMinute(datetime)
Param
datetime: TimeReturn value
Example
SELECT toMinute(toDateTime('2021-08-12 00:00:00'))
┌─toMinute(toDateTime('2021-08-12 00:00:00'))─┐ │ 0 │ └─────────────────────────────────────────────┘
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 TimeReturn value
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 │ └─────────────────────────────────────────────┘
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 TimeReturn value
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 │ └─────────────────────────────────────────────┘
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 TimeReturn value
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 │ └──────────────────────────────────────────────┘
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 TimeReturn value
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 │ └─────────────────────────────────────────────────────┘
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 TimeReturn value
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 │ └──────────────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └────────────────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └───────────────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └─────────────────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └────────────────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └──────────────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └──────────────────────────────────────────────────────┘
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: DateTimeReturn value
Example
SELECT toSecond(toDateTime('2021-08-12 00:00:00'))
┌─toSecond(toDateTime('2021-08-12 00:00:00'))─┐ │ 0 │ └─────────────────────────────────────────────┘
Round datetime down to the start time of the day.
Syntax
toStartOfDay(datetime)
Param
datetime: DateTimeReturn value
Example
SELECT toStartOfDay(toDateTime('2021-08-12 08:00:00'))
┌─toStartOfDay(toDateTime('2021-08-12 08:00:00'))─┐ │ 2021-08-12 00:00:00 │ └─────────────────────────────────────────────────┘
Round datetime down to the start time of the fifteen-minute intervals.
Syntax
toStartOfFifteenMinutes(datetime)
Param
datetime: DateTimeReturn value
Example
SELECT toStartOfFifteenMinutes(toDateTime('2021-08-12 00:10:00'))
┌─toStartOfFifteenMinutes(toDateTime('2021-08-12 00:10:00'))─┐ │ 2021-08-12 00:00:00 │ └────────────────────────────────────────────────────────────┘
Rounds down a DateTime value to the start time of a five-minute interval.
Syntax
toStartOfFiveMinute(datetime)
Param
datetim: DateTimeReturn value
Example
SELECT toStartOfFiveMinute(toDateTime('2021-08-12 00:09:00'))
┌─toStartOfFiveMinute(toDateTime('2021-08-12 00:09:00'))─┐ │ 2021-08-12 00:05:00 │ └────────────────────────────────────────────────────────┘
Rounds down a DateTime value to the start of the hour.
Syntax
toStartOfHour(datetime)
Param
datetimeDateTimeReturn value
Example
SELECT toStartOfHour(toDateTime('2021-08-12 01:09:00'))
┌─toStartOfHour(toDateTime('2021-08-12 01:09:00'))─┐ │ 2021-08-12 01:00:00 │ └──────────────────────────────────────────────────┘
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/dateTimeReturn value
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 │ └─────────────────────────────────────────────────────┘
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/DateTimex unitx is UInt*, the unit can be seconds, minutes, hours, day, month, year, or quartersReturn value
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 │ └───────────────────────────────────────────────────────────────────────────┘
Rounds down a DateTime value to the start of the minute.
Syntax
toStartOfMinute(datetime)
Param
datetimeDateTimeReturn value
Example
SELECT toStartOfMinute(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfMinute(toDateTime('2021-08-12 00:01:30'))─┐ │ 2021-08-12 00:01:00 │ └────────────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └───────────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └─────────────────────────────────────────────────────┘
Rounds down a DateTime value to the start time of a ten-minute interval.
Syntax
toStartOfTenMinutes(datetime)
Param
datetimeDateTimeReturn value
Example
SELECT toStartOfTenMinutes(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfTenMinutes(toDateTime('2021-08-12 00:01:30'))─┐ │ 2021-08-12 00:00:00 │ └────────────────────────────────────────────────────────┘
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/DatetimemodeRefer to week mode.Return value
Example
SELECT toStartOfWeek(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfWeek(toDateTime('2021-08-12 00:01:30'))─┐ │ 2021-08-08 │ └──────────────────────────────────────────────────┘
Round down the date or datetime to the first day of the year.
Returns the date.
Syntax
toStartOfYear(date) toStartOfYear(datetime)
Parameters
date/datetimeDate/DatetimeReturn value
Example
SELECT toStartOfYear(toDateTime('2021-08-12 00:01:30'))
┌─toStartOfYear(toDateTime('2021-08-12 00:01:30'))─┐ │ 2021-01-01 │ └──────────────────────────────────────────────────┘
Convert a datetime to a fixed date while retaining the time.
Syntax
toTime(date) toTime(datetime)
Parameters
date/datetime: Date/DatetimeReturn value
Example
SELECT toTime(toDateTime('2021-08-12 00:01:30'))
┌─toTypeName(toTime(toDateTime('2021-08-12 00:01:30')))─┐ │ DateTime('PRC') │ └───────────────────────────────────────────────────────┘
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
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.
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.
| | | | | \
| Mode | The first day of the week | Range | The first week is the first week ... | |
|---|---|---|---|---|
| \ | ||||
| 0 | Sunday | 0-53 | The year has a Sunday | |
| \ | ||||
| 1 | Monday | 0-53 | The year has 4 days or more | |
| \ | ||||
| 2 | Sunday | 1-53 | The year has a Sunday | |
| \ | ||||
| 3 | Monday | 1-53 | The year has 4 days or more | |
| \ | ||||
| 4 | Sunday | 0-53 | The year has 4 days or more | |
| \ | ||||
| 5 | Monday | 0-53 | This year has one Monday | |
| \ | ||||
| 6 | Sunday | 1-53 | This year has 4 days or more | |
| \ | ||||
| 7 | Monday | 1-53 | This year has one Monday | |
| \ | ||||
| 8 | Sunday | 1-53 | Contains January 1 | |
| \ | ||||
| 9 | Monday | 1-53 | Contains January 1 |
For mode values meaning "This year has 4 days or more", the number of weeks is numbered according to ISO 8601:1988:
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
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 │ └────────────┴───────┴───────┴───────┘
Convert date or datetime to a UInt32 number containing year and month (YYYY * 100 + MM).
Syntax
toYYYYMM(date) toYYYYMM(datetime)
Parameters
date/datetime Date/DateTimeReturn value
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 │ └─────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └───────────────────────────────────────────────┘
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/DateTimeReturn value
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 │ └─────────────────────────────────────────────────────┘
Convert date or datetime to a UInt16 number containing the year (AD).
Syntax
toYear(date) toYear(datetime)
Parameters
date/datetime Date/DateTimeReturn value
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 │ └───────────────────────────────────────────┘
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/DateTimemode Refer to week mode.Return value
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 │ └────────────┴───────────┴───────────┴───────────┘
Accepts zero parameters and returns the current date at the moment the request is executed.
Equivalent to 'toDate(now())'.
Syntax
today()
Return value
Example
SELECT today()
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
dateReturn type
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 │ └───────────────────────┘
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
exprReturn type
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 │ └───────────────────────────────────────────────┘
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
dateReturn type
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 │ └───────────────────────────────────────┘
warning
MySQL-compatible syntax, recommended for use in relevant dialects.
utc_date returns the UTC date.
Syntax
utc_date()
Return type
Example
SELECT utc_date();
┌─utc_date()─┐ │ 2024-07-26 │ └────────────┘
warning
MySQL-compatible syntax, recommended for use in relevant dialects.
utc_time returns the UTC time.
Syntax
utc_time()
Return type
Example
SELECT utc_time();
┌───utc_time()─┐ │ 09:49:58.000 │ └─────────────┘
warning
MySQL-compatible syntax, recommended for use in relevant dialects.
utc_timestamp returns the UTC timestamp.
Syntax
utc_timestamp()
Return type
Example
SELECT utc_timestamp();
┌──────UTCTimestamp()─┐ │ 2024-07-26 09:50:19 │ └─────────────────────┘
Alias of ToWeek
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
dateReturn type
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 │ └───────────────────────┘
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
dateReturn type
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 │ └────────────────────────────┘
Alias of toYear
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/datetimeReturn value
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 │ └────────────┴───────────┴───────────┴───────────┘
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
Example
SELECT yesterday()