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