HIVE中用到的日期函数总结

一、 hive获取当前时间

>select current_date from dual;  -- 2019-03-15
>select current_timestamp from dual; --2019-03-15 15:47:25
  • hive 查询当月第几天
>select dayofmonth(current_date) dual; 
>15 
  • hive 查询当前日期所在月月末
>select last_day(current_date) dual;
>2019-03-31
  • hive 查询 当月第一天日期
>select date_sub(current_date,dayofmonth(current_date)-1) dual;
>2019-03-01
  • hive 查询 下个月第一天日期
>select add_months(date_sub(current_date,dayofmonth(current_date)-1),1) dual;
>2019-04-01

二、 impala中 获取当前时间

>substr(regexp_replace(cast(now() as string),'-',''),1,8) --20190315
>select from_unixtime(unix_timestamp())
>2019-03-15 15:45:56    --通过获取时间戳的形式来获取时间,然后通过函数FROM_UNIXTIME 转化为日期时间格式
  • impala获取之前两天的日期
>select regexp_replace(to_date(date_sub(from_unixtime(unix_timestamp()),2)),'-','')
>20190313 --返回的是string类型的
  • impala获取当前日期所在月
>select substr(regexp_replace(to_date(from_unixtime(unix_timestamp())),'-',''),1,6)
>201903 --获取年份类似,截取日期前四位 substr(date,1,4)

三、 impala中格式化日期

from_unixtime(bigint unixtime, [string format])
-- Return type: string

eg: from_unixtime(1552620898, 'yyyyMMdd') 值是 20190315;
from_unixtime(1552620898,"yyyy-MM-dd") 值是 2019-03-15

四、 hive常用的日期函数

1 hive日期增加函数 date_add (例如获取days天之后的日期 )

date_add(string startdate, days)

>select date_add('2019-03-15',1) from  dual;
>2019-03-16 00:00:00    --返回 TIMESTAMP型 开始日期startdate增加days天后的日期日期

在这里插入图片描述
2 hive日期减少函数 date_sub (获取之前days天的日期)

date_sub (string startdate,days)

>select date_sub('2019-03-15',3) from  dual;
>2019-03-12 00:00:00    --返回 TIMESTAMP型 开始日期startdate减少days天后的日期日期

3 hive日期转年函数 year(string date)

>select year('2019-03-15 16:03:01') from dual;
>2019  --返回 int型的日期值

4 hive日期转月函数 month(string date)

>select month('2019-03-15 16:03:01') from dual;
>3 --返回值int型,日期中的月份

5 hive日期转天函数 day(string date)

>select day('2019-03-15 16:03:01') from dual;
>15 --返回值int型,日期所在天

6 hive日期转周函数 weekofyear(string date)

>select weekofyear('2019-03-15 16:03:01') from dual;
>11 --返回值int型,日期所在周

7 hive日期比较函数 datediff(string enddate,string startdate)

>select datediff('2018-12-08','2019-03-15')from dual;
>-97 --返回值int型,结束日期减去开始日期的天数

8 hive日期时间转日期函数 to_date(string timestamp)

>select to_date('2019-03-15 17:17:48') from dual;
>2019-03-15 --返回string型的日期
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值