最近写sql,经常会使用到hive的这些时间计算函数,记录下来方便大家以后使用:
-- 前一天
select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1)
-- 当前周一
select date_add(next_day(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),'MO'),-7)
select regexp_replace(date_add(next_day(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),'MO'),-7),'-','') -- yyyymmdd
-- 当前周天
select date_add(current_date(),7 - case when dayofweek(current_date()) = 1 then 7 else dayofweek(current_date()) - 1