hive日期函数使用大全

本文详细列举了Hive中的日期处理函数,包括字符串拼接、截取、时间戳转换、日期区间获取、中英文日期转换等操作。特别指出Hive不支持to_char函数,且只识别年-月-日格式。示例中展示了如何使用这些函数进行日常工作中常见的日期操作,例如获取上个月的第一天和最后一天、计算日期间隔等。
摘要由CSDN通过智能技术生成

以下hive日期函数百分百满足日常工作使用,请仔细阅读!!

【hive 日期函数 大全】Hive常用日期函数整理
 
注意:1)  hive 没有 to_char函数  2) HIVE 日期函数只识别 年--日 不能识别 年-月 ,所以处理月份的时候需要特殊处理
 
1)hive 字符创拼接:
CONCAT(string A, string B…)
SELECT CONCAT('2019','05','11');
2) 字符截取
select substr(add_months(from_unixtime((unix_timestamp('2015-09','yyyy-MM')),'yyyy-MM-dd'),-1),0,7);  
SELECT SUBSTR(from_unixtime((unix_timestamp('201509','yyyyMM')),'yyyyMMdd'),0,6);
SELECT SUBSTR(from_unixtime((unix_timestamp('201509','yyyyMM')),'yyyy-MM-dd'),0,7);
 
hive (felix)> SELECT from_unixtime(unix_timestamp(cast('201509' as string),'yyyyMM'),'yyyy-MM-dd') ;
2015-09-01
Time taken: 0.06 seconds, Fetched: 1 row(s)
hive (felix)> SELECT from_unixtime(unix_timestamp(cast('201509' as string),'yyyyMM'),'yyyy-MM') ;
2015-09
 
select from_unixtime(unix_timestamp(add_months(from_unixtime((unix_timestamp('201509','yyyyMM')),'yyyy-MM-dd'),-1),'yyyy-MM-dd'),'YYYYMM') time_list
 
 
3) 时间区间获取
 
4) 中英文日期转换
hive (default)> select from_unixtime((unix_timestamp('201509','yyyyMM')),'MMM-YY');
OK
Sep-15
Time taken: 0.06 seconds, Fetched: 1 row(s)
 
hive (default)> SELECT from_unixtime(unix_timestamp('Sep-19','MMM-YY'),'yyyyMM');
OK
201812
---英文日期大写
hive (default)> select upper(from_unixtime((unix_timestamp('201509','yyyyMM')),'MMM-YY'));
OK
SEP-15
Time taken: 0.062 seconds, Fetched: 1 row(s)
 
 
 
1.unix_timestamp()
返回当前时区的unix时间戳
返回类型:bigint
hive (default)> SELECT UNIX_TIMESTAMP();
 
2.from_unixtime(bigint unixtime[,string format])
时间戳转日期函数
返回类型:string
hive (default)> select from_unixtime(unix_timestamp(),'yyyyMMdd') ;
20160614
 
3.unix_timestamp(string date)
返回指定日期格式的的时间戳
返回类型:bigint
注意:如果后面只有date参数,date的形式必须为'yyyy-MM-dd HH:mm:ss'的形式。
hive (default)> select unix_timestamp('2020-05-01');
NULL
hive (default)> select unix_timestamp('2020-05-01 00:00:00');
1464710400
 
4.unix_timestamp(string date,string pattern)
返回指定日期格式的时间戳
返回类型:bigint
hive (default)> select unix_timestamp('2020-05-01','yyyy-MM-dd');
1449331200
 
5.to_date(string date)
返回时间字段中的日期部分 必须是yyyy-MM-dd格式
返回类型:string
 
说明: 返回日期时间字段中的日期部分。只能识别到 “年--日” 级别的时间,无法识别 “年-月” 级别的时间。
--1) 注意 使用年月,to_date 日期是识别不了的
hive (default)> select to_date('2016-09');  --有问题,结果为null
OK
NULL 
Time taken: 0.067 seconds, Fetched: 1 row(s)
 
hive (default)> select add_months('2020-05-01',-1);
OK
2020-04-01
 
hive (default)> select to_date('2020-05-01 00:00:00') ;
2020-05-01
 
hive (default)> select to_date('2020-05-01');
2020-05-01
 
 
6.year(string date)
返回时间字段中的年
返回类型:int
hive (default)> select year('2020-05-01 00:00:00') ;
2016
hive (default)> select year('2020-05-01') ;
2016
 
7.month(string date)
返回时间字段中的月
返回类型:int
hive (default)> select month('2020-05-01') ;
6
hive (default)> select month('2020-05-01');
6
 
8.day(string date)
返回时间字段中的天
返回类型:int
hive (default)> select day('2020-05-01') ;
1
 
9day:返回日期中的天
 
select day('2015-04-13 11:32:12');
输出:13
 
10hour:返回日期中的小时
 
select hour('2015-04-13 11:32:12');
输出:11
 
11minute:返回日期中的分钟
 
select minute('2015-04-13 11:32:12');
输出:32
 
12second:返回日期中的秒
 
select second('2015-04-13 11:32:56');
输出:56
 
 
13.weekofyear(string date)
返回时间字段是本年的第多少周
返回类型:int
hive (default)> select weekofyear('2020-05-01') ;
22
 
14.datediff(string enddate,string begindate)
返回enddate与begindate之间的时间差的天数
返回类型:int
hive (default)> select datediff('2020-05-01','2016-05-01') ;
31
--DEMO 2 
hive (default)> SELECT datediff('2020-05-01 00:00:00','2016-05-01 23:00:00');
OK
31
 
15.date_add(string date,int days)
返回date增加days天后的日期
返回类型:string
hive (default)> select date_add('2020-05-01',15) ;
2020-05-16
 
16.date_sub(string date,int days)
返回date减少days天后的日期
返回类型:string
hive (default)> select date_sub('2020-05-01',15) ;
2016-05-17
 
17:Hive中取最近30天数据
datediff(CURRENT_TIMESTAMP ,gmt_create)<=30 
 
18、Hive中 两个日期相差多少小时
select (unix_timestamp('2018-05-25 12:03:55') - unix_timestamp('2018-05-25 11:03:55'))/3600
输出:1
19、Hive中 两个日期相差多少分钟
select (unix_timestamp('2018-05-25 12:03:55') - unix_timestamp('2018-05-25 11:03:55'))/60
输出:60
 
20、hive 计算某一个日期属于星期几,如2018-05-20 是星期日
SELECT IF(pmod(datediff('2018-05-20', '1920-01-01') - 3, 7)='0', 7, pmod(datediff('2018-05-20', '1920-01-01') - 3, 7)) 
输出:7
 
 
21、hive返回上个月第一天和最后一天
--上个月第一天
select trunc(add_months('2020-05-08',-1),'MM');  --只能识别 年-月-日 
select trunc(add_months(CURRENT_TIMESTAMP,-1),'MM')
 
hive (default)> select trunc(add_months('2020-05',-1),'MM'); 
OK
NULL
Time taken: 0.067 seconds, Fetched: 1 row(s)
hive (default)> select trunc(add_months('2020-05-08',-1),'MM'); 
OK
2020-04-01
Time taken: 0.079 seconds, Fetched: 1 row(s)
hive (default)> 
 
 
 
select concat(substr(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),1,7),'-01'); 
 
--上个月最后一天
select date_sub(trunc(CURRENT_TIMESTAMP,'MM'),1);
 
--返回上个月日期 
select substr(add_months(from_unixtime((unix_timestamp('2015-09','yyyy-MM')),'yyyy-MM-dd'),-1),0,7);
 
SELECT substr(from_unixtime(unix_timestamp('2020-05','yyyy-MM'),'yyyyMM'),0,4);
  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值