Presto 时间、日期及计算相关日期

由于工作中在数据迁移,大数据平台数据查询引擎使用Presto,和传统的数据库时间函数有区别,整理一版,供大家参考,一起学习,有错误欢迎指正。

1、查询当前日期
    select current_date;
2、查询当前时间
    select current_timestamp;
                 _col0                 
    ---------------------------------------
     2022-01-02 20:45:58.551 Asia/Shanghai
3、求当前时区
    select current_timezone( );
             _col0          
        ---------------
         Asia/Shanghai
4、将字符串转为日期
    select date('2021-05-31');
5、将UNIX时戳转换为时间戳:from_unixtime(unixtime) → timestamp
    select FROM_UNIXTIME(1475996660);
              _col0          
    -------------------------
    2016-10-09 15:04:20.000
6、将UNIX时戳转换成时戳变量.可以带时区选项:from_unixtime(unixtime, string) → timestamp with time zone
    select from_unixtime(1617256617000/1000,'Asia/Shanghai')
                     _col0                     
    ---------------------------------------
     2021-04-01 13:56:57.000 Asia/Shanghai 
7、每月1号零时
    select date_trunc('month',now());
                     _col0                 
        ---------------------------------------
         2022-01-01 00:00:00.000 Asia/Shanghai 
8、每月1号
    select date_trunc('month',current_date);
           _col0    
        ------------
         2022-01-01 
9、每月15号9点30分
    select date_trunc('month',now()) + interval '15' day + interval '9' hour + interval '30' minute;
                 _col0                 
    ---------------------------------------
     2022-01-16 09:30:00.000 Asia/Shanghai
10、每天零时
    select to_char(date_trunc('day',now()),'yyyy-mm-dd hh:mi:ss');
            _col0        
    ---------------------
     2022-01-03 00:00:00
11、求每周一是哪天
    select date_trunc('week',now());
                     _col0                 
    ---------------------------------------
     2022-01-03 00:00:00.000 Asia/Shanghai 
    
    select date_trunc('week',date('2021-12-05'));
       _col0    
    ------------
     2021-11-29 
12、求每小时
    select date_trunc('hour',now());
                     _col0                 
    ---------------------------------------
     2022-01-03 09:00:00.000 Asia/Shanghai 
13、求当前时间截止到分
    select date_trunc('minute',now());
                 _col0                 
    ---------------------------------------
     2022-01-03 09:59:00.000 Asia/Shanghai 
14、求当前时间
    select to_char(date_trunc('minute',now()),'yyyy-mm-dd hh:mi:ss');
            _col0        
    ---------------------
     2022-01-03 10:02:00
15、求今年第一天
    select date_trunc('year',now());
                     _col0                 
    ---------------------------------------
     2022-01-01 00:00:00.000 Asia/Shanghai 
16、时间格式转换
    例子:当前时间20200110 转化为2020-01-10
    select format_datetime(date_parse('20200110','%Y%m%d'),'yyyy-MM-dd');
17、时间的加减
    select date_add('day',6,date'2021-05-07');
    select date_add('day',6,cast('2021-05-07' as date));
    select date_add('day',7,cast(format_datetime(date_parse('20211223','%Y%m%d'),'yyyy-MM-dd') AS DATE));
    select date_add('day',7,date(format_datetime(date_parse('20211223','%Y%m%d'),'yyyy-MM-dd')));
    月份和年,直接搞day换成相应的month和year
18、时间戳转日期
    select from_unixtime(1589213441);
    修改下格式:
    1、select date(from_unixtime(1589213441));
    2、select format_datetime(from_unixtime(1589213441),'yyyy-MM-dd');
19、日期转换为时间戳
    select to_unixtime(cast('2020-01-10' as date));
    select to_unixtime(cast(format_datetime(date_parse('20211224','%Y%m%d'),'yyyy-MM-dd') as date));
20、计算两个日期之间的天数
    select date_diff('day',date('2021-11-23'),date('2021-12-23'));
21、计算本月第一个天、上月第一天、上月最后一天    
    select date_trunc('month',current_date), --本月第一天
           date_add('month',-1,date_trunc('month',current_date)),--上月月初
           date_add('day',-1,date_trunc('month',current_date)) --上月最后一天
       _col0    |   _col1    |   _col2    
    ------------+------------+------------
     2022-01-01 | 2021-12-01 | 2021-12-31 
22、求求上年年初、去年今天、今年年初、当月月初、当前时间
    select date_trunc('month',date_add('month',-12,current_date)), --求上年年初
           date_add('month',-12,current_date), --去年今天
           date_trunc('year',current_date), --今年年初
           date_trunc('month',current_date), --当月月初
           current_date; --当前时间

关于第16、17的一点小说明:

1、把第16个换成下面的写法:

select format_datetime(date_parse('20200110','%Y%m%d'),'YYYY-MM-DD');

这么写没有问题,可以得到想要的结果:

   _col0    
------------
 2020-01-10 

但是换成下面的形式,就会有问题

select format_datetime(date_parse('20211111','%Y%m%d'),'YYYY-MM-DD');

    _col0    
-------------
 2021-11-315

看到没,2021年11月份有315天,这个原因是:YYYY-MM-DD,建议大家,写日期格式的时候,使用:yyyy-MM-dd,也就是除了月份大写外,其他的全部小写。

2、select date_add('day',7,cast(format_datetime(date_parse('20211111','%Y%m%d'),'YYYY-MM-DD') as date)) ;

这个执行报错,可能看了半天也会觉得没问题,错误如下:

Query 20220103_091408_00107_n5kjc failed: Value cannot be cast to date: 2021-11-315

原因就是第一点说的,日期转换的时候,大写了,换成下面的就可以了:

select date_add('day',7,cast(format_datetime(date_parse('20211111','%Y%m%d'),'yyyy-MM-dd') as date));

   _col0    
------------
 2021-11-18 

你可能感兴趣的:(presto,big,data,presto)