hive 时间转字符串_hive日期函数

2023-05-16

hive的函数有很多,今天我带大家来总结下hive中常用的日期函数吧!!!

如有不足,还请大家多多指出,希望能和大家一起交流,共同进步!

03b43daac44978b21bdfe6a2f31e1e92.png

时间一点一滴在溜走,我们要珍惜每一刻

1.日期时间转日期函数:to_date()

to_date(string timestamp) 返回日期时间字段中的日期部分。

返回类型:string

select to_date('2011-12-08 10:03:01') --2011-12-08

2.获取当前日期:current_date()

current_date() 返回当前时间日期

返回类型:date

select current_date() --2019-02-14

3.查询当前系统时间(包括毫秒数): current_timestamp()

current_timestamp() 返回当前时间戳

返回类型:timestamp

select current_timestamp() --2019-02-14 18:50:50.241

4.日期增加函数:date_add()

date_add(string startdate, int days) 返回开始日期startdate增加days天后的日期

返回类型:string

select date_add('2018-12-31', 1) --2019-01-01

5.日期减少函数:date_sub()

date_sub (string startdate, int days) 返回开始日期startdate减少days天后的日期

返回类型:string

select date_sub('2018-12-31', 1) --2018-12-30

6.日期比较函数:datediff()

datediff(string enddate, string startdate) 返回结束日期减去开始日期的天数

返回类型:int

select datediff('2019-02-15','2019-02-01') --14

7.日期格式化,按照格式返回字符串:date_format()

date_format(date/timestamp/string, string fmt) 按指定格式返回date

返回类型: string

select date_format('2019-04-08 12:12:12','yyyy-MM-dd') --2019-04-08select date_format('2019-04-08 12:12:12','yyyyMMdd') --20190408select date_format('2019-04-08 12:12:12','yyyy-MM') --2019-04select date_format('2019-04-08 12:12:12','yyyy') --2019

8.日期转年函数:year()

year(string/date) 返回时间字符串的年份部分

返回类型:int

select year('2019-04-08 12:12:12') --2019

9.月份函数:month()

month(string/date) 返回时间字符串的月份

返回类型:int

select month('2019-04-11') --4

10.天函数:day() /dayofmonth(date)

day(string/date) 返回时间字符串的天

返回类型:int

select day('2019-04-08 12:12:12') --8select day('2019-04-11') --11select dayofmonth('2019-04-08 12:13:11') --8

11.小时函数:hour()

hour(string/date) 返回时间字符串的小时数字

返回类型:int

select hour('2019-04-08 12:13:11') --12

12.分钟函数:minute()

minute(string/date) 返回时间字符串的分钟数字

返回类型:int

select minute('2019-04-08 12:13:11') --13

13.秒函数:second()

second(string/date) 返回时间字符串的分钟数字

返回类型:int

select second('2019-04-08 12:13:11') --11

14.月份差:months_between()

months_between(date1, date2) 返回date1与date2之间相差的月份,如date1>date2,则返回正,如果date1

返回类型:double

select months_between('2019-02-25','2019-01-26') --0.96774194select months_between('2019-02-25','2019-02-26') --0.03225806select months_between('2019-02-25','2019-02-25') --0

15.增加月份:add_months()

add_months(string start_date, int num_months) 返回当前时间下再增加num_months个月的日期

返回类型:string

select add_months('2019-01-01',2) --2019-03-01

16.查询时间字符串位于一年中的第几个周内:weekofyear()

weekofyear(string/date) 返回时间字符串位于一年中的第几个周内

返回类型:int

select weekofyear('2019-04-08 12:13:11') --15

17.查询当月第几天: dayofmonth(current_date)

返回类型:int

select dayofmonth(current_date()) --14

18.返回月末: last_day()

last_day(string date) 返回这个月的最后一天的日期,忽略时分秒部分(HH:mm:ss)

返回类型:string

select last_day(current_date()) --2019-02-28

19.返回时间的最开始年份或月份 :trunc()

trunc(string date, string format) 返回时间的最开始年份或月份

返回类型:string

select trunc('2019-04-08','YY') --2019-01-01select trunc('2019-04-08','MM') --2019-04-01
abe419f896b216812aebb54143e9b4b6.png

诗和远方

20.返回当月第1天:

有多种方法可以灵活使用,这里我列出我经常使用的两种方法

1).trunc(current_timestamp(),'MM')

select trunc(current_timestamp(),'MM') ----2019-02-01

2).date_sub(current_date,dayofmonth(current_date)-1)

select date_sub(current_date,dayofmonth(current_date)-1) --2019-02-01

21.返回下个月第1天:

1).trunc(add_months(current_timestamp(),1),'MM')

select trunc(add_months(current_timestamp(),1),'MM') --2019-03-01

2). add_months(date_sub(current_date,dayofmonth(current_date)-1),1)

select add_months(date_sub(current_date,dayofmonth(current_date)-1),1) --2019-03-01

22.返回上个月第一天

1). trunc(add_months(current_timestamp(),-1),'MM')

select trunc(add_months(current_timestamp(),-1),'MM') --2019-01-01

2). add_months(date_sub(current_date,dayofmonth(current_date)-1),-1)

select add_months(date_sub(current_date,dayofmonth(current_date)-1),-1) --2019-01-01

23. 下周几的具体日期: next_day()

next_day(string date, string week) 返回当前时间的下一个星期X所对应的日期

返回类型:string

下周一:select next_day(to_date(CURRENT_TIMESTAMP),'MO') --2019-02-18本周一:select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),7) --2019-02-11上周一:select date_sub(next_day(to_date(CURRENT_TIMESTAMP),'MO'),14) --2019-02-04
430918d65b021f6f9da6b430037c9a22.png

工作之余,小酌一杯吧

24.UNIX时间戳转日期函数:from_unixtime()

from_unixtime(bigint unixtime[, string format]) 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

返回类型:string

select from_unixtime(1323308143,'yyyy-MM-dd') --2011-12-08

25.获取当前UNIX时间戳函数: unix_timestamp()

返回类型: bigint

select unix_timestamp() --1554721853

26.日期转UNIX时间戳函数: unix_timestamp()

unix_timestamp(string date) 转换格式为“yyyy-MM-dd HH:mm:ss“的日期到UNIX时间戳。如果转化失败,则返回0

返回类型: bigint

 select unix_timestamp('2019-03-07 13:01:03') --1551934863

27.指定格式日期转UNIX时间戳函数: unix_timestamp

unix_timestamp(string date, string pattern) 转换pattern格式的日期到UNIX时间戳。如果转化失败,则返回0

返回类型: bigint

unix_timestamp('2009-03-20', 'yyyy-MM-dd') --1553011200

28.hive中获取当前时间

from_unixtime(unix_timestamp(),'yyyy-MM-dd HH:mm:ss')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

hive 时间转字符串_hive日期函数 的相关文章

随机推荐