HIVE上与时间相关的操作

---------------------------------------------hive中时间相关的函数操作------------------

-- 把指定的字符串转时间 -年月(返回值错误)
 select from_unixtime(unix_timestamp('201806', 'yyyymm'),'yyyymm');

-- 把指定字符串转时间 -年月日
select from_unixtime(unix_timestamp('20180605', 'yyyymmdd'),'yyyymmdd');

-- 把指定字符串转时间 -年月,再进行运算,运算结束后再转回字符串年月
select substr(regexp_replace(string(add_months(from_unixtime(unix_timestamp('201806', 'yyyyMM'),'yyyy-MM-dd'),-2)),"-",""),1,6)

----对于yyyy-mm-dd hh:mi:ss格式的string1 
使用select from_unixtime(unix_timestamp(string1 , 'yyyymmdd'),'yyyymmdd') 的查询结果是错误的。
需要使用
select from_unixtime(unix_timestamp(string1 , 'yyyy-mm-dd'),'yyyy-mm-dd')
--- 系统当前时间 (日期格式)
select from_unixtime(unix_timestamp() , 'yyyyMM'); 
select from_unixtime(unix_timestamp() , 'yyyyMMdd');
-- 系统当前时间 转字符串
select string(from_unixtime(unix_timestamp() , 'yyyyMM'));
select string(from_unixtime(unix_timestamp() , 'yyyyMMdd'));

---- 系统年月做运算:年-月-日
select add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-2);   --- 20201209
select add_months(from_unixtime(unix_timestamp(), 'yyyyMMdd'),-2);     --- null

-- --系统年月做运算, 得到string类型年月
select substr(regexp_replace(string(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-2)), '-','') , 1 , 6);

-- --系统年月做运算, 得到string类型年月日
select substr(regexp_replace(string(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-2)), '-','') , 1 , 8);

------用系统timestamp得到时间格式-年月
select from_unixtime(unix_timestamp(substr(regexp_replace(string(add_months(from_unixtime(unix_timestamp(), 'yyyy-MM-dd'),-2)), '-','') , 1 , 6), 'yyyymm'),'yyyymm');
 
-- 把hive中的timestamp类型变量转化为字符串类型yyyymm变量
select substr(regexp_replace(string(execute_date), '-','') , 1 , 6) from 用户名.表名;
-- 把hive中的timestamp类型变量转化为字符串类型yyyymmdd变量
select substr(regexp_replace(string(execute_date), '-','') , 1 , 6) from 用户名.表名;


---- hive 中把timestamp转化为date
select to_date(execute_date) from 用户名.表名; 

------------

---- 获取当前日期
select current_date;

-----获取当前时间的第二天

select date_add(current_date , 1 );
-- 获取下一个月
select add_months(current_date,1);

-- 根据系统日期按日运算yyyymmdd格式的string
select  substr(regexp_replace(string(date_add(current_date , 1 )) ,'-',''),1,8);


----
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值