Hive中日期与时间戳转换

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/BAStriver/article/details/82977953

1.时间戳转成日期

select distinct  from_unixtime(1441565203,'yyyy/MM/dd HH:mm:ss') from test_date; 

2.日期转成时间戳

select distinct unix_timestamp('20111207 13:01:03') from test_date; // 默认格式为“yyyy-MM-dd HH:mm:ss“

select distinct unix_timestamp('20111207 13:01:03','yyyyMMdd HH:mm:ss') from test_date;   

3.yyyymmdd和yyyy-mm-dd日期之间的切换

方法1: from_unixtime+ unix_timestamp
--20181205转成2018-12-05 
select from_unixtime(unix_timestamp('20181205','yyyymmdd'),'yyyy-mm-dd') from dual;

--2018-12-05转成20181205
select from_unixtime(unix_timestamp('2018-12-05','yyyy-mm-dd'),'yyyymmdd') from dual;

方法2: substr + concat
--20181205转成2018-12-05 
select concat(substr('20181205',1,4),'-',substr('20181205',5,2),'-',substr('20181205',7,2)) from dual;

--2018-12-05转成20181205
select concat(substr('2018-12-05',1,4),substr('2018-12-05',6,2),substr('2018-12-05',9,2)) from dual;

猜你喜欢

转载自blog.csdn.net/BAStriver/article/details/82977953