Hive日期函数使用

year

返回日期的年份数

hive> select year('2021-06-21 16:45:46');
      OK
      2021

month

返回日期的月份值

hive> select month('2021-12-21');
      OK
      12

day

返回日期的当前天数

hive> select day('2021-12-21');
      OK
      21

hour

返回日期或时间的小时

hive> select hour('2021-06-21 16:45:46');
      OK
      16

minute

返回时间的分钟值

hive> select minute('16:45:46');
      OK
      45

second

返回日期或时间的秒数

hive> select second('2021-06-21 16:45:46');
      OK
      46

add_months

月份增加

hive> select add_months('2021-02-28'1);
      OK
      2021-03-31

date_add

日期增加

hive> select date_add('2021-06-30'1);
      OK
      2021-07-01

date_sub

日期减少

hive> select date_sub('2021-06-30',1);
      OK
      2021-06-29

date_format

日期格式化

hive> select date_format('2021-06-30','M');
      OK
      6

datediff

求两个日期的差值

hive> select datediff('2021-06-12','2021-06-14');
      OK
      -2
hive> select datediff('2021-06-14','2021-06-12');
      OK
      2

dayofmonth

求年月日的日

hive> select dayofmonth('2021-06-30');
      OK
      30

dayofweek

求当前日期是星期几,一周从星期天开始

hive> select dayofweek('2021-06-21');
      OK
      2

weekofyear

返回指定时间是这一年的第几周

hive> select weekofyear('2021-06-21');
      OK
      38

current_date

当前日期

hive> select current_date();
      OK
      2021-06-29

current_timestamp

获取当前时间,精确到毫秒

hive> select current_timestamp();
      OK
      2021-06-29 16:37:15.721

from_unixtime

from_unix(unix_time,format) 把时间戳转换为时间,只能精确到秒。

hive> select from_unixtime('123123127','yyyy-MM-dd HH:mm:ss');
      OK
      1973-11-26 08:52:07

unix_timestamp

unix_timestamp(date[,pattern]) 把时间转换为时间戳,只能精确到秒。

hive> select unix_timestamp('2021-06-21 16:40:40','yyyy-MM-dd HH:mm:ss');
      OK
      1624264840

to_unix_timestamp

to_unix_timestamp(date[,pattern]) 把时间转换为时间戳,只能精确到秒。

hive> select to_unix_timestamp('2021-06-21 16:40:40','yyyy-MM-dd HH:mm:ss');
      OK
      1624264840

to_utc_timestamp

to_utc_timestamp(timestamp,string timezone) 支持毫秒精度的时间戳,需要指定时区

hive> select to_utc_timestamp('1559461463324','GMT');
      OK
      2019-06-02 15:44:23.324

from_utc_timestamp

from_utc_timestamp(timestamp,string timezone) 支持毫秒精度的时间戳,需要指定时区

hive> select from_utc_timestamp('1559461463324','GMT');
      OK
      2019-06-02 15:44:23.324

next_day

next_day(start_date,day_of_week) 求当前日期的下一个周几

hive> select current_date();
      OK
      2021-07-19
hive> select next_day(current_date(),'mo');
      OK
      2021-07-26
hive> select next_day(current_date(),'tu');
      OK
      2021-07-20
hive> select next_day(current_date(),'we');
      OK
      2021-07-21
hive> select next_day(current_date(),'th');
      OK
      2021-07-22
hive> select next_day(current_date(),'fr');
      OK
      2021-07-23
hive> select next_day(current_date(),'sa');
      OK
      2021-07-24
hive> select next_day(current_date(),'su');
      OK
      2021-07-25

last_day

last_day(date) 返回某个月的最后一天

hive> select last_day('2021-08-01');
      OK
      2021-08-31

months_between

months_between(‘date1’,‘date2’) 返回两个日期之间的月份差,date1如果比date2日期晚,返回正数,反之,返回负数。

hive> select months_between('2021-06-04','20211-05-04');
      OK
      1.0
hive> select months_between('2021-03-04','20211-05-04');
      OK
      -2.0

to_date

to_date(expr)

hive> select to_date('2021-08-01 04:14:13');
      OK
      2021-08-01

trunc

trunc(date,fmt) date:日期时间类型 fmt:MONTH/MON/MM OR YEAR/YYYY/YY 截断指定格式,后补初始时间,如果为YEAR则返回year(date)-01-01,如果为MM则返回year(date)+month(date)-01

hive> select trunc('2016-10-27','YEAR');
      OK
      2016-01-01
hive> select trunc('2016-10-27','MM');
      OK
      2016-10-01
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Hive提供了一些日期函数,可以方便地对日期进行计算和操作。要获取昨天的日期,可以使用Hive内置的日期函数来实现。可以使用date_sub函数来减去指定的天数,从而获取昨天的日期。具体的使用方法如下所示: ```sql SELECT date_sub(current_date(), 1) AS yesterday; ``` 这个查询会返回昨天的日期。其中,current_date()函数用于获取当前日期,date_sub函数将当前日期减去1天,得到昨天的日期。运行以上查询后,你将得到昨天的日期作为查询结果。 此外,你也可以使用date_add函数来实现相同的效果。使用date_add函数时,你需要指定负数作为第二个参数,表示减去指定的天数。以下是使用date_add函数的示例查询: ```sql SELECT date_add(current_date(), -1) AS yesterday; ``` 这个查询也会返回昨天的日期。在这个示例中,current_date()函数用于获取当前日期,date_add函数将当前日期减去1天,得到昨天的日期。运行以上查询后,你将得到昨天的日期作为查询结果。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [hive日期函数](https://blog.csdn.net/moose_killer/article/details/124389184)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [Hive自定义函数](https://download.csdn.net/download/weixin_38733333/14885908)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值