Clickhouse 时间函数操作总结 —— Clickhouse 基础篇(八)

当前时间

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:002023-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:462024 │
└─────────────────────┴──────┘

计算季度

SELECT
    now() AS x,
    toQuarter(x) AS y

Query id: 0f1ab49a-8769-4220-87c5-de136845a1f8

┌───────────────────x─┬─y─┐
│ 2024-05-15 19:36:282 │
└─────────────────────┴───┘

计算月份

SELECT
    now() AS x,
    toMonth(x) AS y

Query id: e9e5d096-62b6-4884-a9fc-9339d92b88c8

┌───────────────────x─┬─y─┐
│ 2024-05-15 19:37:015 │
└─────────────────────┴───┘

计算该年中第几天

SELECT
    now() AS x,
    toDayOfYear(x) AS y

Query id: 77e50368-947b-4c19-8a64-a90b8fe23ec9

┌───────────────────x─┬───y─┐
│ 2024-05-15 19:37:52136 │
└─────────────────────┴─────┘

计算该月中第几天

SELECT
    now() AS x,
    toDayOfMonth(x) AS y

Query id: 2e08d9bb-5053-4401-92fa-67a4f6a1b5dd

┌───────────────────x─┬──y─┐
│ 2024-05-15 19:38:3515 │
└─────────────────────┴────┘

计算该周中第几天

SELECT
    now() AS x,
    toDayOfWeek(x) AS y

Query id: b014f2b9-12c2-4eee-ba72-252f42efa5d2

┌───────────────────x─┬─y─┐
│ 2024-05-15 19:39:073 │
└─────────────────────┴───┘

计算小时

SELECT
    now() AS x,
    toHour(x) AS y

Query id: dab5b335-2d19-45ba-be58-ad833c4de319

┌───────────────────x─┬──y─┐
│ 2024-05-15 19:39:4019 │
└─────────────────────┴────┘

计算分钟

SELECT
    now() AS x,
    toMinute(x) AS y

Query id: a0691b48-e0b7-4c7e-b92f-5e38950e1cc6

┌───────────────────x─┬──y─┐
│ 2024-05-15 19:40:1140 │
└─────────────────────┴────┘

计算秒

SELECT
    now() AS x,
    toSecond(x) AS y

Query id: 4e6463ed-92e0-4512-a913-36cd3bcde934

┌───────────────────x─┬──y─┐
│ 2024-05-15 19:40:4444 │
└─────────────────────┴────┘

计算UNIX时间戳

SELECT
    now() AS x,
    toUnixTimestamp(x) AS y

Query id: 994b381d-0658-4221-932e-a1f96a807b25

┌───────────────────x─┬──────────y─┐
│ 2024-05-15 19:41:171715773277 │
└─────────────────────┴────────────┘

时间加法

第一个参数表示时间单位,可取值为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 │
└─────┘
  • 4
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一切如来心秘密

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值