获取指定日期的周初和周末:
select date_sub(now(),dayofweek(now())-1-1) weekstart,date_sub(now(),dayofweek(now())-1-7) weekend;
-- 时间戳取整
Impala 2.11 之前的取整当前时间的写法:
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 秒
Impala 2.11 之后增加了 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
日期格式转换:
with dt as (
select '7/15/2020' as dt
union all select '7/1/2020'
union all select '12/31/2020'
union all select '1/1/2020'
)
select dt, concat(split_part(dt,"/",3),"-",case when length(split_part(dt,"/",1))=1 then lpad(split_part(dt,"/",1),2,"0") else split_part(dt,"/",1) end,"-"
,case when length(split_part(dt,"/",2))=1 then lpad(split_part(dt,"/",2),2,"0") else split_part(dt,"/",2) end ) "YYYY-MM-DD"
from dt;
--impala获取当前日期(方法1)
substr(regexp_replace(cast(now() as string),'-',''),1,8)
--impala获取当前日期(方法2)
concat(
cast(year(now()) as string),
lpad(cast(month(now()) as string),2,'0'),
lpad(cast(day(now()) as string),2,'0')
)