暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

Hive 时间相关函数汇总

数据原子 2021-08-14
4626
      使用hive进行时间处理的过程中,经常会用到一些时间函数,现整理一下hive中常用日期函数,做备忘录查询使用


1、取得当前日期时间:

    --取得当前日期:
    select current_date();
    输出:2021-08-14
    --取得当前日期时间:
    select current_timestamp();
    输出:2021-08-14 13:14:57
    --hive取得当前时间戳:
    select unix_timestamp();
    1628911641

    2、to_date:日期时间转日期函数,返回日期时间字段中的日期部分,(字符串必须为:yyyy-MM-dd格式)。

      select to_date('2021-08-14 13:34:12');
      输出:2021-08-14

      3、from_unixtime:unix时间戳到转时间格式

      说明: 转化UNIX时间戳(从1970-01-01 00:00:00 UTC到指定时间的秒数)到当前时区的时间格式

        select from_unixtime(1323308945,’yyyy-MM-dd’);
        输出:2011-12-08
        select from_unixtime(1323308945,’yyyyMMdd’);
        输出:20111208
        --取得当前时间,相当于select current_timestamp();
        select from_unixtime(unix_times
        tamp(),'yyyy-MM-dd HH:dd:ss');


        输出:2021-08-14 03:14:57

        4、date_format:日期、时间戳、字符串类型格式化输出标准时间格式

          select date_format(current_timestamp(),'yyyy-MM-dd HH:mm:ss');
          输出:2021-08-14 11:14:46
          select date_format(current_date(),'yyyy-MM-dd');
          输出:2021-08-14
          select date_format('2021-08-14','yyyy-MM-dd HH:mm:ss'); 
          输出:2021-08-14 00:00:00

          5、unix_timestamp:获取当前时间的unix时间戳和日期转UNIX时间戳函数

            select unix_timestamp();
            输出:1628906623
            select unix_timestamp('2021-08-14 10:05:20');
            输出:1628935520

            6、from_utc_timestamp/to_utc_timestamp:utc时间转换:

              select from_utc_timestamp(current_timestamp(),8);
              输出:2021-08-14 11:10:27.762
              select to_utc_timestamp(current_timestamp(),8);
              输出:2021-08-14 11:10:56.085

              7、to_unix_timestamp:日期转unix时间戳

                select to_unix_timestamp('2021-08-14 11:10:27','yyyy-MM-dd HH:dd:ss');
                输出:1628593227

                8、year/quarter/month/day:返回日期中的年/季度/月/日

                  select year('2021-08-14 10:05:20');
                  输出:2021
                  select quarter('2021-08-14 10:05:20');
                  输出:3
                  select month('2021-08-14 10:05:20');
                  输出:8
                  select day('2021-08-14 10:05:20');
                  输出:14

                  9、hour/minute/second:返回日期中的时/分/秒

                    select hour('2021-08-14 10:05:20');
                    输出:10
                    select minute('2021-08-14 10:05:20');
                    输出:5
                    select second('2021-08-14 10:05:20');
                    输出:20

                    10、weekofyear:返回日期在当年的第几周

                      select weekofyear('2021-08-14 10:05:20');
                      输出:32

                      10、dayofweek:返回日期在当前周的第几天(注:周日为第1天)

                        select dayofweek('2021-08-14 10:05:20');  --周六
                        输出:7

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

                        说明:前者大于后者,返回值为正,否则,返回值为负。

                          select datediff('2021-08-14','2021-08-08');
                          输出:6
                          select datediff(current_date(),date_add(current_date(),-10));
                          输出:10
                          select datediff(current_date(),date_add(current_date(),10));
                          输出:-10

                          12、date_sub:日期减少函数,返回日期前n天的日期

                            select date_sub('2021-08-14',6);
                            输出:2021-08-08

                            13、date_add:日期增加函数,返回日期后n天的日期

                              select date_add('2021-08-14',6);
                              输出:2021-08-20
                              --取得昨天日期:
                              select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);
                              select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
                              -- 取得明天日期:
                              select date_add(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),1);
                              select date_sub(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1);

                              14、months_between:返回两个日期之间包含的月数(结果为double类型)

                                select months_between( '2021-10-14','2021-05-04');
                                输出:5.32258065

                                15、trunc:获取日期月初(参数MM),年初日期(参数YY)

                                  select trunc(current_date(),'MM');
                                  输出:2021-08-01
                                  select trunc(current_date(),'YY');
                                  输出:2021-01-01

                                  16、last_day:获取日期当月最后一天

                                    select last_day(current_date());
                                    输出:2021-08-31

                                    17、next_day:返回当前日期之后的下个星期几的日期

                                    说明:参数为MO,TU,WE,TH,FR,SA,SU,也可以是两个缩写字母可以是三个可以是全名。

                                      select next_day('2021-08-14', 'TU'); --得到021-08-14后的下个周二
                                      输出:2021-08-17
                                      select next_date('2020-01-01','Fri'); --得到2020-01-01后的下个周五
                                      输出:2020-01-03

                                      18、from_unixtime+unix_timestamp:yyyymmdd和yyyy-mm-dd日期间切换

                                      思路:先转换成时间戳,再由时间戳转换为对应格式。

                                        select from_unixtime(unix_timestamp('20210814','yyyymmdd'),'yyyy-mm-dd');
                                        输出:2021-08-14
                                        select from_unixtime(unix_timestamp('2021-08-14','yyyy-mm-dd'),'yyyymmdd') ;
                                        输出:20210814

                                        19、取最近30天数据

                                          seelct datediff(current_timestamp(),create_time)<=30;

                                          20、两个日期相差多少小时

                                            select (unix_timestamp('2021-08-14 10:18:54')-unix_timestamp('2021-08-14 08:18:54'))/3600;
                                            输出:2.0

                                            21、两个日期相差多少分钟

                                              select (unix_timestamp('2021-08-14 10:18:54')-unix_timestamp('2021-08-14 08:18:54'))/60
                                              输出:120.0

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

                                                --上个月第一天
                                                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 trunc(add_months(current_timestamp(),-1),'MM');
                                                输出:2021-07-01
                                                --上个月最后一天
                                                select last_day(add_months(from_unixtime(unix_timestamp(),'yyyy-MM-dd'),-1)) ;
                                                select date_sub(trunc(current_timestamp(),'MM'),1);
                                                输出:2021-07-31
                                                -- 获取当月第一天
                                                select trunc(current_timestamp(),'MM');
                                                select from_unixtime(unix_timestamp(),'yyyy-MM-01');
                                                输出:2021-08-01
                                                -- 获取当月最后一天
                                                select last_day(current_timestamp());
                                                select last_day(current_date());
                                                输出:2021-08-31


                                                文章转载自数据原子,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

                                                评论