hive中时间类date函数

1. 日期格式化函数 :date_format(string date,输出格式) 

返回值: string 
说明:  对给定的字符串日期进行格式化。 

举例:

hive> select date_format(current_date,'yyyy-MM-dd HH:mm:ss');
返回结果 2021-03-14 00:00:00

hive> select date_format('2019-06-29','yyyy-MM');
返回结果 2019-06

hive> select date_format('2019/06/29','yyyy-MM-dd');
返回结果 NULL ,说明执行失败。必须将2019/06/29转换成2019-06-29格式才能执行。
hive> select date_format(regexp_replace('2019/06/29','/','-'),'yyyy-MM-dd');
返回结果 :2019-06-29 

2. 日期比较函数: datediff语法: datediff(string enddate,string startdate) 

返回值: int 
说明: 返回结束日期减去开始日期的天数。 

举例:

hive> select datediff('2016-12-30','2016-12-29');

返回结果 1

3. 日期增加函数: date_add语法: date_add(string startdate, intdays) 

返回值: string 
说明: 返回开始日期startdate增加days天后的日期。 intdays可以是负数,就编程了日期减少函数。

举例:

hive>select date_add('2016-12-29',10);

返回结果 2017-01-08

4. 日期减少函数: date_sub语法: date_sub (string startdate,int days) 

返回值: string 

说明: 返回开始日期startdate减少days天后的日期。 

举例:

hive>select date_sub('2016-12-29',10);

返回结果 2016-12-19

4.查询近30天的数据

select * from table where datediff(current_timestamp,create_time)<=30;

create_time 为table里的字段,current_timestamp 返回当前时间 2018-06-01 11:00:00
 

 

 

  • 1
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值