Hive日期函数整理

在使用hive sql进行数据分析和统计的时候经常会使用到日期函数。所以现在想把日期函数整理一下。方便自己后期查看使用

时间辍转换函数

unix_timestamp(string date)

  • 直接执行select unix_timestamp()获取当前时间辍。
  • 如果参数date满足yyyy-MM-dd HH:mm:ss形式,则可以直接unix_timestamp(string date) 得到参数对应的时间戳
  • 如果参数date满足yyyy-MM-dd HH:mm:ss形式,我们需要指定date的形式,在进行转换select unix_timestamp('2021-06-08', 'yyyy-MM-dd') = 1623081600

from_unixtime(bigint timestamp,string dateformat)

  • 将时间辍转换成指定的dateformat格式。
  • select from_unixtime(1623081600,'yyyy-MM-dd HH:mm:ss') = 2021-06-08 00:00:00
  • 可以不指定dateformat格式。默认为yyyy-MM-dd HH:mm:ss。select from_unixtime(1623081600) = 2021-06-08 00:00:00
  • 将时间辍指定dateformat格式。select from_unixtime(unix_timestamp(),'yyyyMMdd HH:mm:ss') = 20210608 17:17:25
日期计算函数 三个date函数日期均只能为'yyyy-MM-dd'格式 & 'yyyy-MM-dd HH:mm:s'格式

date_sub(string date, int n/-m)

  • 返回初始日期n天前、m天后的日期。
  • select date_sub('2021-06-08 20:00:00',5) = 2020-06-03
  • select date_sub('2021-06-08 20:00:00',-2) = 2020-06-10

datediff(string date1, string date2)

  • 返回前后日期之间的天数差
  • select datediff('2021-06-08','2021-06-02') = 6
  • select datediff('2021-06-03','2021-06-08') = -5

date_add(string date, int n/-m)

  • 返回初始日期n天后、m天前的日期。
  • select date_add('2021-06-08 20:00:00',5) = 2020-06-13
  • select date_add('2021-06-08 20:00:00',-2) = 2020-06-06

to_date(string date)

  • 日期时间转日期函数
  • select to_date('2021-06-08 09:03:00') = 2021-06-08
posted @ 2021-06-08 17:18  数月亮  阅读(670)  评论(0编辑  收藏  举报