Hive Date Functions 日期函数

这篇博客介绍了Apache Hive中用于日期和时间处理的函数,包括`unix_timestamp`、`from_unixtime`、`date_format`、`date_add`、`date_sub`等。通过示例展示了如何进行日期加减、格式转换以及日期差计算,对于数据处理和分析工作具有实用价值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官网参考 :LanguageManual UDF - Apache Hive - Apache Software Foundation

常见的用法:

--string '20220715' 减去一天 '20220714'
select regexp_replace(date_sub(from_unixtime(unix_timestamp('20220715','yyyyMMdd'),'yyyy-MM-dd'),1),'-','')

select date_format(date_add(from_unixtime(unix_timestamp('20220715','yyyyMMdd'),'yyyy-MM-dd'),-1),'yyyyMMdd');

--  unix_timestamp()   将日期date转化为整型的UNIX格式的日期时间值。
--	unix_timestamp(string date)
--  unix_timestamp(string date, string pattern)
select unix_timestamp();    --1657849929
select unix_timestamp('2022-07-14 17:48:26');   --1657792106;
select unix_timestamp('20220714','yyyyMMdd');  --1657728000

-- to_date(string timestamp)    返回类型: pre 2.1.0: string  2.1.0 on: date	
select to_date('2022-07-14 17:48:26');  --2022-07-14
select to_date('2022-07-14');           --2022-07-14
select to_date('20220714');             -- NULL

--from_unixtime:转化unix时间戳到当前时区的时间格式   返回类型:  string
select from_unixtime(1657792106);               -- 2022-07-14 17:48:26
select from_unixtime(1657792106,'yyyy-MM-dd');  -- 2022-07-14
select from_unixtime(1657792106,'yyyyMMdd');    --20220714

--date_format(date/timestamp/string ts, string fmt)   返回类型:  string
select date_format('2022-07-14','yyyyMMdd'); --20220714
select date_format('2022-07-14','yyyy');     --2022

-- date_add(date/timestamp/string startdate, tinyint/smallint/int days)  返回类型: pre 2.1.0: string  2.1.0 on: date	
select date_add('2022-07-31',1);   --2022-08-01
select date_add('2022-07-31',-1);  --2022-07-30

-- date_sub(date/timestamp/string startdate, tinyint/smallint/int days)  返回类型: pre 2.1.0: string  2.1.0 on: date	
select date_sub('2022-07-31',1);   --2022-07-30
select date_sub('2022-07-31',-1);  --2022-08-01

--datediff(string enddate, string startdate)
select datediff('2022-07-15','2022-07-14');  -- 1
select datediff('20220715','20220714');      -- NULL

--interval
select to_date('2022-07-14') - interval 3 day;  --2022-07-11 00:00:00
select current_timestamp()  + INTERVAL 1 year;  --2023-07-15 10:55:09.941

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值