栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

Hive常用时间函数小结

Hive常用时间函数小结

–日期 转换 时间戳
select unix_timestamp() --1640140182 bigint 当前时间戳

select unix_timestamp(‘2021-12-22 10:29:42’,‘yyyy-MM-dd hh:mm:ss’) --1640140182 bigint
select unix_timestamp(‘2021-12-22 10:29:42’);–1640140182 bigint 满足’yyyy-MM-dd hh:mm:ss’格式的,一个参数就可以
select unix_timestamp(‘2021-12-22’,‘yyyy-MM-dd’) – 1640102400 big int 不满足时间格式的,要指定格式

– 时间戳 转换 日期 返回 string 类型的时间
select from_unixtime(unix_timestamp(),‘yyyy-MM-dd hh:mm:ss’) as dt – string
select from_unixtime(1640140182,‘yyyy-MM-dd hh:mm:ss’) as date_time – string ‘2021-12-22 10:29:42’
select from_unixtime(1640140182,‘yyyy-MM-dd’) as dt – string ‘2021-12-22’
–特殊:如果unixtime为13位的,需要先转成10位
select from_unixtime(cast(1640140182488/1000 as int),‘yyyy-MM-dd HH:mm:ss’) – string ‘2021-12-22 10:29:42’
select from_unixtime(cast(substr(1640140182488,1,10) as int),‘yyyy-MM-dd HH:mm:ss’) – string ‘2021-12-22 10:29:42’

–日期字符串与日期相互转化
select to_date(‘2021-12-22 10:29:42’) – date 2021-12-22 字符串转换为日期
select cast(current_date() as string) – 直接将日期转化为 字符串 ‘2022-03-03’
select date_format(‘2021-12-22 10:29:42’,‘yyyy-MM-dd’) – string '2021-12-22’转换日期字符串格式

select current_date(); – date 2022-03-03 当前日期
select date_sub(current_date(),1); – date 2021-03-02 日期减去天数,几天前,返回的是日期
select date_add(current_date(),1); – date 2021-03-04 几天后,日期加天数,返回的是日期
select datediff(current_date(),to_date(‘2022-03-02 10:29:42’)); – int 两个日期差的天数

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/751497.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号