分享

Hive各种时间格式处理




yyyy-MM-dd与yyyyMMdd000000转换的三种方法


方法一:date_format(只支持yyyy-MM-dd -> yyyyMMdd000000)

select date_format('2019-10-07', 'yyyyMMdd000000')
-- 20191007000000


方法二:from_unixtime + unix_timestamp

select from_unixtime(unix_timestamp('2019-10-07', 'yyyy-MM-dd'), 'yyyyMMdd000000')
-- 20191007000000

select from_unixtime(unix_timestamp(substr('20191007000000',1,8),'yyyyMMdd'),'yyyy-MM-dd')
-- 2019-10-07


方法三:substr + concat

select concat(substr('2019-10-07',1,4),substr('2019-10-07',6,2),substr('2019-10-07',9,2),'000000')
-- 20191007000000

select concat(substr('20191007000000',1,4),'-',substr('20191007000000',5,2),'-',substr('20191007000000',7,2))
-- 2019-10-07


时间转换方法详解


unix_timestamp:格式化日期转时间戳

select unix_timestamp('2019-10-07 13:24:20','yyyy-MM-dd HH:mm:ss')
-- 1570425860

select unix_timestamp('20191007','yyyyMMdd')
-- 1570377600


from_unixtime:时间戳转格式化日期

select from_unixtime(1570425860,'yyyy-MM-dd HH:mm:ss')
-- 2019-10-07 13:24:20

select from_unixtime(1570425860,'yyyyMMdd000000')
-- 20191007000000


date_format:yyyy-MM-dd HH:mm:ss 时间转格式化时间

select date_format('2019-10-07 13:24:20', 'yyyyMMdd000000')
-- 20191007000000

select date_format('2019-10-07', 'yyyyMMdd000000')
-- 20191007000000


yyyy-MM-dd HH:mm:ss 注意
MM为月份

mm为分钟

HH为24小时制

hh为12小时制


本帖被以下淘专辑推荐:

已有(4)人评论

跳转到指定楼层
yunge2016 发表于 2020-3-16 08:25:33
怎么把hive表里的字段转换为这种类型呀, 我转换后一直是NULL, 能给看看吗
用的这个 from_unixtime:时间戳转格式化日期 , 转换后就是null了.
20200316083445.png

点评

可以试试这个 select from_unixtime( cast(1564581347793/1000 as int),'yyyy-MM-dd HH:mm:ss') ;  发表于 2020-3-16 09:42
回复

使用道具 举报

yunge2016 发表于 2020-3-28 09:10:25
yunge2016 发表于 2020-3-16 08:25
怎么把hive表里的字段转换为这种类型呀, 我转换后一直是NULL, 能给看看吗
用的这个 from_unixtime:时间戳 ...

谢谢您的回复哦, 已经解决啦。 非常感谢。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条