当前时间
SELECT now()
Query id: aa741313-218d-426f-8afc-d78c7d744d16
┌───────────────now()─┐
│ 2024-05-15 19:29:35 │
└─────────────────────┘
今天日期
SELECT today()
Query id: c46b2045-1464-4bc5-b794-8d60ae0b7bd6
┌────today()─┐
│ 2024-05-15 │
└────────────┘
昨天日期
SELECT yesterday()
Query id: b3d30ea7-e189-42c4-83bd-ec73fc62b49f
┌─yesterday()─┐
│ 2024-05-14 │
└─────────────┘
当前时区
SELECT timeZone()
Query id: a4ba52d6-9917-43da-b7d1-593aaeac2c28
┌─timeZone()────┐
│ Asia/Shanghai │
└───────────────┘
时区转换
- 将时间或带日期的时间转换为指定的时区的时间。
SELECT
toDateTime('2023-01-01 00:00:00', 'UTC') AS utc_time,
toTimeZone(utc_time, 'Asia/Shanghai') AS shanghai_time
Query id: 0788d287-2917-491a-9e1e-48e09c434b98
┌────────────utc_time─┬───────shanghai_time─┐
│ 2023-01-01 00:00:00 │ 2023-01-01 08:00:00 │
└─────────────────────┴─────────────────────┘
计算年份
SELECT
now() AS x,
toYear(x) AS y
Query id: 6fbaa0a8-bd88-4e42-824e-d196236e04f7
┌───────────────────x─┬────y─┐
│ 2024-05-15 19:35:46 │ 2024 │
└─────────────────────┴──────┘
计算季度
SELECT
now() AS x,
toQuarter(x) AS y
Query id: 0f1ab49a-8769-4220-87c5-de136845a1f8
┌───────────────────x─┬─y─┐
│ 2024-05-15 19:36:28 │ 2 │
└─────────────────────┴───┘
计算月份
SELECT
now() AS x,
toMonth(x) AS y
Query id: e9e5d096-62b6-4884-a9fc-9339d92b88c8
┌───────────────────x─┬─y─┐
│ 2024-05-15 19:37:01 │ 5 │
└─────────────────────┴───┘
计算该年中第几天
SELECT
now() AS x,
toDayOfYear(x) AS y
Query id: 77e50368-947b-4c19-8a64-a90b8fe23ec9
┌───────────────────x─┬───y─┐
│ 2024-05-15 19:37:52 │ 136 │
└─────────────────────┴─────┘
计算该月中第几天
SELECT
now() AS x,
toDayOfMonth(x) AS y
Query id: 2e08d9bb-5053-4401-92fa-67a4f6a1b5dd
┌───────────────────x─┬──y─┐
│ 2024-05-15 19:38:35 │ 15 │
└─────────────────────┴────┘
计算该周中第几天
SELECT
now() AS x,
toDayOfWeek(x) AS y
Query id: b014f2b9-12c2-4eee-ba72-252f42efa5d2
┌───────────────────x─┬─y─┐
│ 2024-05-15 19:39:07 │ 3 │
└─────────────────────┴───┘
计算小时
SELECT
now() AS x,
toHour(x) AS y
Query id: dab5b335-2d19-45ba-be58-ad833c4de319
┌───────────────────x─┬──y─┐
│ 2024-05-15 19:39:40 │ 19 │
└─────────────────────┴────┘
计算分钟
SELECT
now() AS x,
toMinute(x) AS y
Query id: a0691b48-e0b7-4c7e-b92f-5e38950e1cc6
┌───────────────────x─┬──y─┐
│ 2024-05-15 19:40:11 │ 40 │
└─────────────────────┴────┘
计算秒
SELECT
now() AS x,
toSecond(x) AS y
Query id: 4e6463ed-92e0-4512-a913-36cd3bcde934
┌───────────────────x─┬──y─┐
│ 2024-05-15 19:40:44 │ 44 │
└─────────────────────┴────┘
计算UNIX时间戳
SELECT
now() AS x,
toUnixTimestamp(x) AS y
Query id: 994b381d-0658-4221-932e-a1f96a807b25
┌───────────────────x─┬──────────y─┐
│ 2024-05-15 19:41:17 │ 1715773277 │
└─────────────────────┴────────────┘
时间加法
第一个参数表示时间单位,可取值为second、minute、hour、day、week、month、quarter、year。
081c89ba798d :) select dateAdd(YEAR, 1, now());
SELECT now() + toIntervalYear(1)
Query id: 7dd4b393-c2af-4c62-8657-835b5cbccefa
┌─plus(now(), toIntervalYear(1))─┐
│ 2025-05-15 19:47:49 │
└────────────────────────────────┘
时间减法
第一个参数表示时间单位,可取值为second、minute、hour、day、week、month、quarter、year。
081c89ba798d :) select dateSub(YEAR, 1, now());
SELECT now() - toIntervalYear(1)
Query id: 06e3fb52-bd9e-47ca-a9a2-0ac3bf2e4f0c
┌─minus(now(), toIntervalYear(1))─┐
│ 2023-05-15 19:46:42 │
└─────────────────────────────────┘
计算相差天数
第一个参数为时间单位,可取值为second、minute、hour、day、week、month、quarter、year。
SELECT dateDiff('day', toDateTime('2023-01-01 00:00:00'), now()) AS res
Query id: f5a3f32e-481d-49a7-95a8-8f9ba028e3c4
┌─res─┐
│ 500 │
└─────┘