zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    numpy、torch:[ ]里传函数,数组过滤
    torch:torch.sort
    torch : transpose,transpose_
    托管堆和垃圾回收笔记
    UdpClient类客户端和服务端demo
    JavaScript阻止事件冒泡
    单元测试初接触
    CSS选择器
    文档onload处理程序
    TASKKILL命令使用方法
  • 原文地址:https://www.cnblogs.com/gemine/p/14863676.html
Copyright © 2011-2022 走看看