注:所有的时间日期函数都可以在第二个可选参数中接受时区参数。示例:Asia / Yekaterinburg。在这种情况下,它们使用指定的时区而不是本地(默认)时区。

  1. SELECT
  2. toDateTime('2016-06-15 23:00:00') AS time,
  3. toDate(time) AS date_local,
  4. toDate(time, 'Asia/Yekaterinburg') AS date_yekat,
  5. toString(time, 'US/Samoa') AS time_samoa
  1. ┌────────────────time─┬─date_local─┬─date_yekat─┬─time_samoa──────────┐
  2. 2016-06-15 23:00:00 2016-06-15 2016-06-16 2016-06-15 09:00:00
  3. └─────────────────────┴────────────┴────────────┴─────────────────────┘

常用时间函数:

  1. now() // 2020-04-01 17:25:40 取当前时间
  2. toYear() // 2020 取日期中的年份
  3. toMonth() // 4 取日期中的月份
  4. today() // 2020-04-01 今天的日期
  5. yesterday() // 2020-03-31 昨天的额日期
  6. toDayOfYear() // 92 取一年中的第几天
  7. toDayOfWeek() // 3 取一周中的第几天
  8. toHour() //17 取小时
  9. toMinute() //25 取分钟
  10. toSecond() //40 取秒
  11. toStartOfYear() //2020-01-01 取一年中的第一天
  12. toStartOfMonth() //2020-04-01 取当月的第一天
  13. formatDateTime(now(),'%Y-%m-%d') // 2020*04-01 指定时间格式
  14. toYYYYMM() //202004
  15. toYYYYMMDD() //20200401
  16. toYYYYMMDDhhmmss() //20200401172540
  17. dateDiff()
  18. ......
  1. SELECT
  2. toDateTime('2019-07-30 10:10:10') AS time,
  3. -- DateTime转换成Unix时间戳
  4. toUnixTimestamp(time) as unixTimestamp,
  5. -- 保留 时-分-秒
  6. toDate(time) as date_local,
  7. toTime(time) as date_time, -- DateTime中的日期转换为一个固定的日期,同时保留时间部分。
  8. -- 获取年份,月份,季度,小时,分钟,秒钟
  9. toYear(time) as get_year,
  10. toMonth(time) as get_month,
  11. -- 一年分为四个季度。1(一季度:1-3),2(二季度:4-6),3(三季度:7-9),4(四季度:10-12
  12. toQuarter(time) as get_quarter,
  13. toHour(time) as get_hour,
  14. toMinute(time) as get_minute,
  15. toSecond(time) as get_second,
  16. -- 获取 DateTime中的当前日期是当前年份的第几天,当前月份的第几日,当前星期的周几
  17. toDayOfYear(time) as "当前年份中的第几天",
  18. toDayOfMonth(time) as "当前月份的第几天",
  19. toDayOfWeek(time) as "星期",
  20. toDate(time, 'Asia/Shanghai') AS date_shanghai,
  21. toDateTime(time, 'Asia/Shanghai') AS time_shanghai,
  22. -- 得到当前年份的第一天,当前月份的第一天,当前季度的第一天,当前日期的开始时刻
  23. toStartOfYear(time),
  24. toStartOfMonth(time),
  25. toStartOfQuarter(time),
  26. toStartOfDay(time) AS cur_start_daytime,
  27. toStartOfHour(time) as cur_start_hour,
  28. toStartOfMinute(time) AS cur_start_minute,
  29. -- 从过去的某个固定的时间开始,以此得到当前指定的日期的编号
  30. toRelativeYearNum(time),
  31. toRelativeQuarterNum(time);

获取未来时间的函数:

  1. -- 第一种,日期格式(指定日期,需注意时区的问题)
  2. WITH
  3. toDate('2019-09-09') AS date,
  4. toDateTime('2019-09-09 00:00:00') AS date_time
  5. SELECT
  6. addYears(date, 1) AS add_years_with_date,
  7. addYears(date_time, 0) AS add_years_with_date_time;
  8. -- 第二种,日期格式(当前,本地时间)
  9. WITH
  10. toDate(now()) as date,
  11. toDateTime(now()) as date_time
  12. SELECT
  13. now() as now_time,-- 当前时间
  14. -- 之后1
  15. addYears(date, 1) AS add_years_with_date,
  16. addYears(date_time, 1) AS add_years_with_date_time,
  17. -- 之后1
  18. addMonths(date, 1) AS add_months_with_date,
  19. addMonths(date_time, 1) AS add_months_with_date_time,
  20. --之后1
  21. addWeeks(date, 1) AS add_weeks_with_date,
  22. addWeeks(date_time, 1) AS add_weeks_with_date_time,
  23. -- 之后1
  24. addDays(date, 1) AS add_days_with_date,
  25. addDays(date_time, 1) AS add_days_with_date_time,
  26. --之后1小时
  27. addHours(date_time, 1) AS add_hours_with_date_time,
  28. --之后1分中
  29. addMinutes(date_time, 1) AS add_minutes_with_date_time,
  30. -- 之后10秒钟
  31. addSeconds(date_time, 10) AS add_seconds_with_date_time,
  32. -- 之后1个季度
  33. addQuarters(date, 1) AS add_quarters_with_date,
  34. addQuarters(date_time, 1) AS add_quarters_with_date_time;

获取过去时间的函数:

  1. WITH
  2. toDate(now()) as date,
  3. toDateTime(now()) as date_time
  4. SELECT
  5. subtractYears(date, 1) AS subtract_years_with_date,
  6. subtractYears(date_time, 1) AS subtract_years_with_date_time,
  7. subtractQuarters(date, 1) AS subtract_Quarters_with_date,
  8. subtractQuarters(date_time, 1) AS subtract_Quarters_with_date_time,
  9. subtractMonths(date, 1) AS subtract_Months_with_date,
  10. subtractMonths(date_time, 1) AS subtract_Months_with_date_time,
  11. subtractWeeks(date, 1) AS subtract_Weeks_with_date,
  12. subtractWeeks(date_time, 1) AS subtract_Weeks_with_date_time,
  13. subtractDays(date, 1) AS subtract_Days_with_date,
  14. subtractDays(date_time, 1) AS subtract_Days_with_date_time,
  15. subtractHours(date_time, 1) AS subtract_Hours_with_date_time,
  16. subtractMinutes(date_time, 1) AS subtract_Minutes_with_date_time,
  17. subtractSeconds(date_time, 1) AS subtract_Seconds_with_date_time;
  18. SELECT toDate('2019-07-31', 'Asia/GuangZhou') as date_guangzhou;
  19. SELECT toDate('2019-07-31'), toDate('2019-07-31', 'Asia/Beijing') as date_beijing;
  20. -- 亚洲只能加载上海的timezone???
  21. SELECT toDateTime('2019-07-31 10:10:10', 'Asia/Shanghai') as date_shanghai;

计算连个时刻在不同时间单位下的差值

  1. -- 第一种:指定时间计算差值示例
  2. WITH
  3. toDateTime('2019-07-30 10:10:10', 'Asia/Shanghai') as date_shanghai_one,
  4. toDateTime('2020-10-31 11:20:30', 'Asia/Shanghai') as date_shanghai_two
  5. SELECT
  6. dateDiff('year', date_shanghai_one, date_shanghai_two) as diff_years,
  7. dateDiff('month', date_shanghai_one, date_shanghai_two) as diff_months,
  8. dateDiff('week', date_shanghai_one, date_shanghai_two) as diff_week,
  9. dateDiff('day', date_shanghai_one, date_shanghai_two) as diff_days,
  10. dateDiff('hour', date_shanghai_one, date_shanghai_two) as diff_hours,
  11. dateDiff('minute', date_shanghai_one, date_shanghai_two) as diff_minutes,
  12. dateDiff('second', date_shanghai_one, date_shanghai_two) as diff_seconds;
  13. -- 第二种:本地当前时间示例
  14. WITH
  15. now() as date_time, -- 当前时间
  16. toStartOfMonth(date_time) as start_of_month, -- 月初
  17. addDays(addMonths(start_of_month, 1), -1) as end_of_month, -- 月末
  18. addMonths(start_of_month, 1) as next_start_of_month, -- 下个月初
  19. addDays(addMonths(next_start_of_month, 1), -1) as next_end_of_month -- 下个月末
  20. SELECT date_time,
  21. start_of_month,
  22. end_of_month,
  23. next_start_of_month,
  24. next_end_of_month,
  25. dateDiff('day', start_of_month, next_start_of_month) as month_diff_days, -- 当月天数
  26. dateDiff('year', date_time, addYears(date_time, 1)) as diff_years, -- 相差年份
  27. dateDiff('month', date_time, addMonths(date_time, 2)) as diff_months, -- 相差月份
  28. dateDiff('week', date_time, addWeeks(date_time, 3)) as diff_week, -- 相差星期
  29. dateDiff('day', date_time, addDays(date_time, 3)) as diff_days, -- 相差天数
  30. dateDiff('hour', date_time, addHours(date_time, 3)) as diff_hours, -- 相差小时
  31. dateDiff('minute', date_time, addMinutes(date_time, 30)) as diff_minutes, -- 相差分钟
  32. dateDiff('second', date_time, addSeconds(date_time, 35)) as diff_seconds -- 相差秒数
  33. ;

参考:


  1. Clickhouse时间日期函数 官方文档
    2. Clickhouse 基础知识 - 函数