Hive获取当前时间-月初-月末-时间差-某月的天数--函数

                                         Hive常用时间函数总结

获取当前时间     >>>     select current_date;

0: jdbc:hive2://linux01:10000> select current_date;
+-------------+
|     _c0     |
+-------------+
| 2020-09-14  |
+-------------+

获取当前时间

0: jdbc:hive2://linux01:10000> select current_timestamp;
+-------------------------+
|           _c0           |
+-------------------------+
| 2020-09-14 17:16:19.88  |
+-------------------------+

获取当前时间戳   >>>    select UNIX_TIMESTAMP();

0: jdbc:hive2://linux01:10000> select UNIX_TIMESTAMP();
+-------------+
|     _c0     |
+-------------+
| 1600074118  |
+-------------+
  1. 获取当前的前1天或前两天   >>>   select date_sub(CURRENT_DATE,1);    
  2. 注:前2天 就将 1 改成2 即可
  3. 注 :后两天  就将 1 改成  -2 就行  可以根据正负进行加减天数    
0: jdbc:hive2://linux01:10000> 
0: jdbc:hive2://linux01:10000> select date_sub(CURRENT_DATE,1);
+-------------+
|     _c0     |
+-------------+
| 2020-09-13  |
+-------------+

现在是当月的第几天

>  select dayofmonth(current_date)    今天是9月22日

0: jdbc:hive2://emr-header-1:10000> select dayofmonth(current_date);
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
+------+
| _c0  |
+------+
| 22   |
+------+

获取当月月份  >>>    select substr(current_date , 1 ,7 );

0: jdbc:hive2://linux01:10000> select substr(current_date , 1 ,7 );
+----------+
|   _c0    |
+----------+
| 2020-09  |
+----------+

获取本月月初第一天   >>>     select date_sub(current_date,dayofmonth(current_date)-1);

0: jdbc:hive2://linux01:10000> 
0: jdbc:hive2://linux01:10000> select date_sub(current_date,dayofmonth(current_date)-1);
+-------------+
|     _c0     |
+-------------+
| 2020-09-01  |
+-------------+

获取本月月末     >>>    select last_day(current_date) ;

0: jdbc:hive2://linux01:10000> 
0: jdbc:hive2://linux01:10000> select last_day(current_date) ;
+-------------+
|     _c0     |
+-------------+
| 2020-09-30  |
+-------------+

查询下个月的第一天     >>>    select add_months(date_sub(current_date,dayofmonth(current_date)-1),1);

0: jdbc:hive2://linux01:10000> select add_months(date_sub(current_date,dayofmonth(current_date)-1),1);
+-------------+
|     _c0     |
+-------------+
| 2020-10-01  |
+-------------+

查询上个月 月份   >>>  select substr(add_months(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd HH:mm:ss'), -1 ) ,1, 7);  

注:如果想查询前6个月等 将 -1 改成 -6 即可,查询 后半年的  将-1 改成 6 即可 

0: jdbc:hive2://linux01:10000>  select substr(add_months(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd HH:mm:ss'), -1 ) ,1, 7);
+----------+
|   _c0    |
+----------+
| 2020-08  |
+----------+

获取上个月的今天    >>>     select add_months(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd HH:mm:ss'), -1 );

注 要是想获取前两个月 就将-1改成 -2 , 

0: jdbc:hive2://linux01:10000> select add_months(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd HH:mm:ss'), -1 );
+-------------+
|     _c0     |
+-------------+
| 2020-08-14  |
+-------------+

获取上个月的最后一天  >>>   select last_day(add_months(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd HH:mm:ss'), -1 ) )

注:想获取得上上个月的月末  可以将 -1 改成 -2 ,可根据你的需要时间进行修改

获取下下个月的 将  -1  改成 2  即可  可根据你的需要时间进行修改

0: jdbc:hive2://linux01:10000> select last_day(add_months(FROM_UNIXTIME(UNIX_TIMESTAMP(),'yyyy-MM-dd HH:mm:ss'), -1 ) ) ;
+-------------+
|     _c0     |
+-------------+
| 2020-08-31  |
+-------------+

获取某月的天数

 select  datediff (last_day("2020-08-22") ,  date_sub("2020-08-22",dayofmonth("2020-08-22")-1)  )  ;

注 current_date

0: jdbc:hive2://emr-header-1:10000> select  datediff (last_day(current_date) ,  date_sub(current_date,dayofmonth(current_date)));
WARNING: Hive-on-MR is deprecated in Hive 2 and may not be available in the future versions. Consider using a different execution engine (i.e. spark, tez) or using Hive 1.X releases.
+------+
| _c0  |
+------+
| 30   |
+------+

注:上面的是先获取当月  月初+月末 时间,求时间差   测试的是9月份的数据

 
 

猜你喜欢

转载自blog.csdn.net/weixin_45592182/article/details/108583050