• Hive学习(12)Hive常用日期函数


    1、hive返回当天三种方式

    select current_date; --返回年月日
    --2017-06-15
    select current_timestamp;	--返回年月日时分秒
    --2017-06-15 19:54:44
    SELECT from_unixtime(unix_timestamp());
    --2017-06-15 19:55:04
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    2、from_unixtime:转化unix时间戳到当前时区的时间格式

    select from_unixtime(1323308943,’yyyyMMdd’);
    --输出:20111208
    
    • 1
    • 2

    3、unix_timestamp:获取当前unix时间戳

    select unix_timestamp();
    --输出:1430816254
    select unix_timestamp('2015-04-30 13:51:20');
    --输出:1430373080
    
    • 1
    • 2
    • 3
    • 4

    4、year:返回日期中的年

    select year('2015-04-02 11:32:12');
    --输出:2015
    
    • 1
    • 2

    5、month:返回日期中的月份

    select month('2015-12-02 11:32:12');
    --输出:12
    
    • 1
    • 2

    6、day:返回日期中的天

    select day('2015-04-13 11:32:12');
    --输出:13
    
    • 1
    • 2

    7、hour:返回日期中的小时

    select hour('2015-04-13 11:32:12');
    --输出:11
    
    • 1
    • 2

    8、minute:返回日期中的分钟

    select minute('2015-04-13 11:32:12');
    --输出:32
    
    • 1
    • 2

    9、second:返回日期中的秒

    select second('2015-04-13 11:32:56');
    --输出:56
    
    • 1
    • 2

    10、weekofyear:返回日期在当前周数

    select weekofyear('2015-05-05 12:11:1');
    --输出:19
    
    • 1
    • 2

    11、datediff:返回开始日期减去结束日期的天数

    select datediff('2015-04-09','2015-04-01');
    --输出:8
    
    • 1
    • 2

    12、date_sub:返回日期前n天的日期

    select date_sub('2015-04-09',4);
    --输出:2015-04-05
    
    • 1
    • 2

    13、date_add:返回日期后n天的日期

    select date_add('2015-04-09',4);
    --输出:2015-04-13
    
    • 1
    • 2

    14、from_unixtime+ unix_timestamp Hive中yyyymmdd和yyyy-mm-dd日期之间的切换

    思想:先转换成时间戳,再由时间戳转换为对应格式。
    --20171205转成2017-12-05 
    select from_unixtime(unix_timestamp('20171205','yyyymmdd'),'yyyy-mm-dd') from dual;
    
    --2017-12-05转成20171205
    select from_unixtime(unix_timestamp('2017-12-05','yyyy-mm-dd'),'yyyymmdd') from dual;
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    15:Hive中取最近30天数据

    datediff(CURRENT_TIMESTAMP ,gmt_create)<=30 
    
    • 1

    16、Hive中 两个日期相差多少小时

    select (unix_timestamp('2018-05-25 12:03:55') - unix_timestamp('2018-05-25 11:03:55'))/3600
    --输出:1
    
    • 1
    • 2

    17、Hive中 两个日期相差多少分钟

    select (unix_timestamp('2018-05-25 12:03:55') - unix_timestamp('2018-05-25 11:03:55'))/60
    --输出:60
    
    • 1
    • 2

    18、hive 计算某一个日期属于星期几,如2018-05-20 是星期日

    SELECT IF(pmod(datediff('2018-05-20', '1920-01-01') - 3, 7)='0', 7, pmod(datediff('2018-05-20', '1920-01-01') - 3, 7)) 
    --输出:7
    
    • 1
    • 2

    19、hive返回上个月第一天和最后一天

    --上个月第一天
    select trunc(add_months(CURRENT_TIMESTAMP,-1),'MM')
    
    select concat(substr(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1),1,7),'-01'); 
    
    --上个月最后一天
    select date_sub(trunc(CURRENT_TIMESTAMP,'MM'),1);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    20、to_date:日期时间转日期函数

    select to_date('2015-04-02 13:34:12');
    --输出:2015-04-02
    
    • 1
    • 2

    21、返回当月的第一天

    select trunc('2016-08-16','MM') 
    --2016-08-01
    
    日期截取:
    TRUNC函数为指定元素而截去的日期值。
    其具体的语法格式如下:
    TRUNC(date[,fmt])
    其中:date 一个日期值
    fmt 日期格式,该日期将由指定的元素格式所截去。忽略它则由最近的日期截去
    
    如果当日日期是:2011-3-18
    select trunc(sysdate) from dual 
    --2011-3-18 今天的日期为2011-3-18
    select trunc(sysdate, 'mm') from dual 
    --2011-3-1 返回当月第一天.
    select trunc(sysdate,'yy') from dual 
    --2011-1-1 返回当年第一天
    select trunc(sysdate,'dd') from dual 
    --2011-3-18 返回当前年月日
    select trunc(sysdate,'yyyy') from dual 
    --2011-1-1 返回当年第一天
    select trunc(sysdate,'d') from dual 
    --2011-3-13 (星期天)返回当前星期的第一天
    select trunc(sysdate, 'hh') from dual 
    --2011-3-18 14:00:00 当前时间为14:41
    select trunc(sysdate, 'mi') from dual 
    --2011-3-18 14:41:00 TRUNC()函数没有秒的精确
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    参考::
    https://blog.csdn.net/u013421629/article/details/80450047
    https://blog.51cto.com/u_12228/6473685

  • 相关阅读:
    【力扣10天SQL入门】Day2
    高级人工智能复习 中科大
    合并零之间非零节点
    python操作mysql数据库
    Spring cache源码分析
    【状语从句练习题】从句时态补充
    rust &String 和 &str 区别
    SD卡报错“error -110 whilst initialising SD card
    NIO Selector选择器
    WebSocket开发web页面与服务实时通信
  • 原文地址:https://blog.csdn.net/Dreamy_zsy/article/details/132567920