Hive获取月份_【Hive】Hive中常用日期函数整理

阿里云教程5小时前发布
1 0 0

https://blog.csdn.net/weixin_39667801/article/details/110165685
http://www.saoniuhuo.com/article/detail-1181.html

to_date:日期时间转日期函数
select to_date( 2015-04-02 13:34:12 );
输出:2015-04-02

from_unixtime:转化unix时间戳到当前时区的时间格式
select from_unixtime(1323308943,’yyyyMMdd’);
输出:20111208

unix_timestamp:获取当前unix时间戳
select unix_timestamp();
输出:1430816254
select unix_timestamp( 2015-04-30 13:51:20 );
输出:1430373080

year:返回日期中的年
select year( 2015-04-02 11:32:12 );
输出:2015

month:返回日期中的月份
select month( 2015-12-02 11:32:12 );
输出:12

day:返回日期中的天
select day( 2015-04-13 11:32:12 );
输出:13

hour:返回日期中的小时
select hour( 2015-04-13 11:32:12 );
输出:11

minute:返回日期中的分钟
select minute( 2015-04-13 11:32:12 );
输出:32

second:返回日期中的秒
select second( 2015-04-13 11:32:56 );
输出:56

weekofyear:返回日期在当前周数
select weekofyear( 2015-05-05 12:11:1 );
输出:19

datediff:返回开始日期减去结束日期的天数
select datediff( 2015-04-09 , 2015-04-01 );
输出:8

date_sub:返回日期前n天的日期
select date_sub( 2015-04-09 ,4);
输出:2015-04-05

date_add:返回日期后n天的日期
select date_add( 2015-04-09 ,4);
输出:2015-04-13

一、参考日期为当前日期

  • 昨天

#方式一:
select date_format(date_sub(current_date(),1),  yyyyMMdd );
#方式二:
select date_format(date_sub(from_unixtime(unix_timestamp(),  yyyy-MM-dd ),1), yyyyMMdd );
#方式三:
select date_format(date_sub(to_date(current_date()),1), yyyyMMdd );

  • 本月

select from_unixtime(unix_timestamp(),  yyyyMM )

  • 本月月初

select CONCAT(from_unixtime(unix_timestamp(),  yyyyMM ), 01 );

  • 上月同期

select date_format(add_months(from_unixtime(unix_timestamp(),  yyyy-MM-dd ),-1), yyyyMMdd );

  • 上月月初

select CONCAT(date_format(add_months(from_unixtime(unix_timestamp(),  yyyy-MM-dd ),-1), yyyyMM ), 01 );

  • 上月月末

select date_format(date_sub(from_unixtime(unix_timestamp(CONCAT(from_unixtime(unix_timestamp(),  yyyyMM ), 01 ),  yyyyMMdd ),  yyyy-MM-dd ),1), yyyyMMdd );

  • 去年同期

select date_format(add_months(from_unixtime(unix_timestamp(),  yyyy-MM-dd ),-12), yyyyMMdd );

  • 去年同期月初

select CONCAT(date_format(add_months(from_unixtime(unix_timestamp(),  yyyy-MM-dd ),-12), yyyyMM ), 01 );

二、参考日期为指定日期: 20210129

  • 昨天

select date_format(date_sub(from_unixtime(unix_timestamp( 20210129 ,  yyyyMMdd ),  yyyy-MM-dd ),1), yyyyMMdd )

  • 本月

select substr( 20210129 ,1,6)

  • 本月月初

select CONCAT(substr( 20210129 ,1,6), 01 )

  • 上月同期

select date_format(add_months(from_unixtime(unix_timestamp( 20210129 ,  yyyyMMdd ),  yyyy-MM-dd ),-1), yyyyMMdd )

  • 上月月初

select CONCAT(date_format(add_months(from_unixtime(unix_timestamp( 20210129 ,  yyyyMMdd ),  yyyy-MM-dd ),-1), yyyyMM ), 01 )

  • 上月月末

select date_format(date_sub(from_unixtime(unix_timestamp(CONCAT(from_unixtime(unix_timestamp( 20210129 ,  yyyyMMdd ),  yyyyMM ), 01 ),  yyyyMMdd ),  yyyy-MM-dd ),1), yyyyMMdd )

  • 去年同期

select date_format(add_months(from_unixtime(unix_timestamp( 20210129 ,  yyyyMMdd ),  yyyy-MM-dd ),-12), yyyyMMdd )

  • 去年同期月初

select CONCAT(date_format(add_months(from_unixtime(unix_timestamp( 20210129 ,  yyyyMMdd ),  yyyy-MM-dd ),-12), yyyyMM ), 01 )

© 版权声明

相关文章

暂无评论

none
暂无评论...