Hive数据库日期格式化

35 篇文章 4 订阅
24 篇文章 2 订阅

1,获取当前日期

select current_date() from table;
返回 :‘2022-06-07’

2,获取当前时间

select current_timestamp() from table;
返回:‘2022-06-07 13:20:00.00’

3,获取当前时间戳

返回类型为int

(1)转换当前的日期为时间戳

select unix_timestamp(current_date()) from table;

(2)转换当前的时间为时间戳

select unix_timestamp(current_timestamp()) from table;

4,时间转换为固定格式类型

函数:from_unixtime()
格式:select from_unixtime(时间戳,时间格式)
示例:
select from_unixtime(1654580056) from table;
返回:‘2022-06-07 13:34:16’

select from_unixtime(1654580056,‘YYYY-MM-dd’) from table;
返回:‘2022-06-07’

select from_unixtime(1654580056,‘YYYYMMdd’) from table;
返回:‘20220607’

select data_format(‘2022-06-07’,‘yyyyMMdd’) from table;
返回:‘20220607’

5,返回日期时间字段中的部分日期

(1)返回日期中的年

select year(‘2022-06-07 13:34:16’) --2022

(2)返回日期中的月

select month(‘2022-06-07 13:34:16’) --06

(3)返回日期中的日

select day(‘2022-06-07 13:34:16’) --07

(4)返回日期中的时

select hour(‘2022-06-07 13:34:16’) --13

(5)返回日期中的分

select minute(‘2022-06-07 13:34:16’) --34

(6)返回日期中的秒

select second(‘2022-06-07 13:34:16’) --16

(7)返回日期中的年月日

select to_date(‘2022-06-07 13:34:16’,‘yyyyMMdd’) from table;

6,返回当月第一天

select trunc(‘2022-06-07’,‘MM’)
–2022-06-01

7,返回当年第一天

select trunc(‘2022-06-07’,‘YEAR’)
–2022-01-01

8,两个日期计算时间差

函数:datediff(开始日期,结束日期)
select datediff(‘2022-06-07’,‘2022-05-11’)
返回:27

9,开始日期增加N天后的日期

函数:date_add(开始日期,天数)
select date_add(‘2022-06-07’,10) from table;
返回:2022-06-17

10,开始日期减少N天后的日期

函数:date_sub(开始日期,天数)
select date_sub(‘2022-06-07’,5) from table;
返回:2022-06-02

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓之以理的喵~~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值