Hive--时间函数大全

1. current_date():获取当前格式化日期

select current_date() as today;

在这里插入图片描述

2. current_timestamp():获取当前格式化时间

select current_timestamp() as today;

在这里插入图片描述

3. unix_timestamp():获取当前unix时间戳

select unix_timestamp() as today;

在这里插入图片描述

4. from_unixtime():把unix时间戳转化为格式化时间

select from_unixtime(1653363939) as today;

在这里插入图片描述
注:第二个参数为时间格式,默认是’yyyy-MM-dd HH:mm:ss’

select from_unixtime(unix_timestamp(),'yyyy-MM-dd') as today;

在这里插入图片描述

5. to_date(): 当前格式化时间(含时分秒)转化为年月日

注:to_date() 默认转化格式为’yyyy-MM-dd’

select to_date('2022-05-23 23:00:01') as today;

在这里插入图片描述

6. date_format(): 对日期进行格式化

select date_format('2022-05-23 23:00:01','yyyy-MM-dd') as today;
select date_format('2022-05-23','yyyy-MM') as month;
select date_format('2022-05-23','yyyy') as year;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

7. year/quarter/month/day/hour/minute/second: 年/季度/月/日/时/分/秒

select year('2022-05-01') as year;
select quarter('2022-05-23') as quarter;
select month('2022-05-01') as month;
select day('2022-05-23') as day;
select hour('2022-05-23 02:00:01') as hour;
select minute('2022-05-23 23:00:01') as minute;
select second('2022-05-23 23:00:01') as second;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

8. date_add(): 取格式化时间的前/后n天

select date_add('2022-05-23',1) as tomorrow;
select date_add('2022-05-23',-7) as lastweek;

在这里插入图片描述
在这里插入图片描述

9. date_sub(): 取格式化时间的前/后n天

select date_sub('2022-05-23',1) as yeasterday;
select date_sub('2022-05-23',-1) as nextday;

在这里插入图片描述
在这里插入图片描述

10. add_months(): 当前时间的前/后n个月

select add_months('2022-05-23',1) as nextmonth;
select add_months('2022-05-23',-1) as lastmonth;

在这里插入图片描述
在这里插入图片描述

11. weekofyear(): 日期转周(当前的日期是一年中的第几周)

select weekofyear('2022-01-01') as 01week;
select weekofyear('2022-01-08') as 08week;

在这里插入图片描述
在这里插入图片描述

12. dayofyear(): 日期转天(当前的日期是一年中的第几天)

13. datediff(): 获取两个时间的天数差值

select datediff('2022-01-08 00:00:01', '2022-02-08 00:02:00') as diff;
select datediff('2022-03-08', '2022-02-08') as difftwo;

在这里插入图片描述
在这里插入图片描述

14. last_day(): 获取指定时间的当月的最后一天

select last_day('2022-02-05') as day;

在这里插入图片描述

15. next_day(): 获取指定时间的下一个星期几

注:next_day()第⼆个参数⽀持⼩写、⼤写、缩写(su/sun/sunday)

select next_day('2022-05-24','su') as nextsunday;
select next_day('2022-05-24','MON') as nextmonday;

在这里插入图片描述
在这里插入图片描述

16. trunc(): 获取当月第一天/获取当年的第一天

select trunc('2022-05-22','YY') as year;
select trunc('2022-05-22','MM') as month;

在这里插入图片描述
在这里插入图片描述

17. 常用时间

I.本周第一天&上周第一天

//2022-05-23是周一
//本周第一天
select date_sub(next_day('2022-05-23','monday'),7) as monday;
//上周第一天
select date_sub(next_day('2022-05-25','monday'),14) as lastmonday;

在这里插入图片描述
在这里插入图片描述

II.本月第一天&本月最后一天&上月第一天

//本月第一天
select trunc('2022-05-31', 'MM') as month;
//本月最后一天
select last_day('2022-02-05') as day;
//上月第一天
select trunc(add_months('2022-05-31',-1), 'MM') as lastmonth;

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值