---------------------------------------------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);
----