美文网首页
impala时间用法

impala时间用法

作者: Quick_5413 | 来源:发表于2021-01-25 16:49 被阅读0次

select to_timestamp('2020-10-02 16:10:20','yyyy-MM-dd HH:mm:ss');
select trunc(now(), 'YEAR') --取整到年份, 得到当年 1 月 1 日 0 点 0 分
select trunc(now(), 'MONTH') --取整到月份, 得到当月 1 日 0 点 0 分
select trunc(now(), 'DD') --取整到日期, 得到当天 0 点 0 分
select trunc(now(), 'DAY') --取整到星期, 得到本星期第一天的 0 点 0 分
select trunc(now(), 'HH24') --取整到小时, 得到当前小时的 0 分
select trunc(now(), 'MI') --取整到分钟, 得到当前分钟 0 秒

date_trunc() 函数, 下面是几个取整的写法:
date_trunc('year',now())
date_trunc('month',now())
date_trunc('week',now())
date_trunc('day',now())
date_trunc('hour',now())
date_trunc('minute',now())
date_trunc() 的语法和 date_part() 类似, 下面是完整的时间 part 列表:
microseconds、milliseconds、second、minute、hour、day、week、month、year、decade、century、millennium

时间戳提取
date_part('year', now())
extract(now(), 'year')
extract(year from now())

两个时间戳比较
datediff(timestamp enddate, timestamp startdate) ,相差多少天, 精度是天
timestamp_cmp(now() + interval 70 minutes, now()), 比较两个时间戳的大小, 本例的结果为 1
时间加减
时间戳可以直接加减 interval n days/months/years/hours/minutes .

也可以使用下面的函数:
years_add(timestamp t, int n)
years_sub(timestamp t, int n)
months_add(timestamp t, int n)
months_sub(timestamp t, int n)
days_add(timestamp t, int n)
days_sub(timestamp t, int n)
hours_add(timestamp t, int n)
hours_sub(timestamp t, int n)
minutes_add(timestamp t, int n)
minutes_sub(timestamp t, int n)
月份相关的
last_day(timestamp t)
months_between(timestamp newer, timestamp older)

相关文章

网友评论

      本文标题:impala时间用法

      本文链接:https://www.haomeiwen.com/subject/qvlizktx.html