hive 中最常用日期处理函数

hive 常用日期处理函数

在工作中,日期函数是提取数据计算数据必须要用到的环节。哪怕是提取某个时间段下的明细数据也得用到日期函数。今天和大家分享一下常用的日期函数。为什么说常用呢?其实这些函数在数据运营同学手上是几乎每天都在使用的。

技术交流

技术要学会分享、交流,不建议闭门造车。一个人可以走的很快、一堆人可以走的更远。

相关文件及代码都已上传,均可加交流群获取,群友已超过2000人,添加时最好的备注方式为:来源+兴趣方向,方便找到志同道合的朋友。

方式①、添加微信号:dkl88194,备注:来自CSDN + 加群
方式②、微信搜索公众号:Python学习与数据挖掘,后台回复:加群

序号hive日期函数函数用法含参方式备注
1to_date转化成日期to_date('string  time')to_date('2023-5-20  05:20:00')
   输出:2023-5-20
2from_unixtime转化unix时间戳到当前时区的时间格式from_unixtime(bigint  unixtime,[string format])select  from_unixtime(1684559640,'yyyyMMdd');
   输出:20230520
3unix_timestamp日期转化为unix时间戳unix_timestamp(string  format)select  unix_timestamp();
   输出:1684559640
   select unix_timestamp('2023-05-20 13:14:00');
   输出:1684559640
4date2datekeydate格式转化成datekeydate2datekey(string  date/time)date2datekey('2023-05-20')
   输出:20230520
5datekey2datedatekey格式转化为datedatekey2date(string  datekey)datekey2date('20230520')
   输出:2023-05-20
6datediff返回开始日期减去结束日期的天数datediff(string  enddate ,string begindate)select  datediff('2023-05-20','2023-05-18');
   输出:2
7date_sub返回日期前n天的日期date_sub(string  startdate,int days )date_sub('2023-05-20',2  )
   输出:2023-05-18
8date_add返回日期后n天的日期date_add(string  startdate,int days )date_add('2023-05-20',2  )
   输出:2023-05-22
9year返回日期中的年year(string  date)year('2023-05-20  11:32:12');
   输出:2023
10month返回日期中的月份month(string  date)month('2023-05-20  11:32:12');
   输出:05
11day返回日期中的天day(string  date)day('2023-05-20  11:32:12');
   输出:20
12hour返回日期中的小时hour(string  date)hour('2023-05-20  11:32:12');
   输出:11
13minute返回日期中的分钟minute(string  date)minute('2023-05-20  11:32:12');
   输出:32
14second返回日期中的秒second(string  date)second('2023-05-20  11:32:12');
   输出:12
15weekofyear返回日期在当前周数weekofyear(string  date)weekofyear('2023-05-20  11:32:12');
   输出:20
16unix_timestamp格式是timestamp,精确到秒unix_timestamp(ymdhms2)  - unix_timestamp(ymdhms1)
   -- 计算2个时间相差的秒数
       (unix_timestamp(time1)-unix_timestamp(time2))
   -- 同理,若是计算相差的分钟,就在以上基础再除以60,小时,天数也是同理
       (unix_timestamp(time1)-unix_timestamp(time2))/60
   -- 根据上述的函数计算后,发现有小数点,可用cast优化为以下
       cast((unix_timestamp(time1)-unix_timestamp(time2))/60 as int)
   -- 相差的秒数。
      CAST((unix_timestamp() - unix_timestamp(ymdhms))  % 60 AS int)
   -- 相差的分钟数。
      CAST((unix_timestamp() -  unix_timestamp(ymdhms)) / 60 AS int) % 60
   -- 相差的小时数。
      CAST((unix_timestamp() - unix_timestamp(ymdhms))  / (60 * 60) AS int) % 24
   -- 相差的天数。
      concat(CAST((unix_timestamp() -  unix_timestamp(ymdhms)) / (60 * 60 * 24) AS int) 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值