Querying using the datepart from a time stamp in a pass-through Proc SQL

后端 未结 3 1980
执笔经年
执笔经年 2021-01-27 15:33

I am trying to use the date part of a time-stamp in my where query in a db2 pass-through proc SQL code below. I tried using date and datepart functions but it wont work with thi

3条回答
  •  攒了一身酷
    2021-01-27 16:15

    If you use a function on the datetime field in DB2 then the database won't be able to use it's indexes (if that field is indexed). This is because indexes are (almost always) created on the field itself, not the result of the field after it has been processed by a function. This holds true for the majority of databases not just DB2.

    Instead, what you want to do is supply datetime values for the beginning of the day and for the end of the day and get everything inbetween them. To simplify this process I created a format called mysqldt.. Originally this format was for a mySQL database, but SQL server and DB2 both use the same formats so it can be used on those as well:

    proc format;
      picture mysqldt low-high = '''%Y-%0m-%0d %0H:%0M:%0S''' (datatype = datetime) ;
    run ;
    

    Once this format is available I tend to use macro variables. At the top of my program I would create a macro variable where I specify the date to use throughout the report:

    %let rpt_date = %sysfunc(mdy(1,12,2013));
    

    I would then create two datetime fields representing the start of the day and the end of the day, and I would save them in the format that is needed for the SQL statement:

    %let sql_start = %sysfunc(dhms(&rpt_date, 0, 0, 0), mysqldt.);
    %let sql_end   = %sysfunc(dhms(&rpt_date,23,59,59), mysqldt.);
    
    %put &rpt_date &sql_start &sql_end;
    

    You would then change your query to look like this:

    proc sql; 
      connect to db2(ssid=smtng); 
      select *  from connection to db2 
             (select *  
              from atable 
              where timestamp between &sql_start and &sql_end
              for read only with ur
        );
    quit; 
    

    This way, not only are your indexes now used in your query, but the SQL looks cleaner and reads easier, and you only need to change the report date in a single place (at the top of your program) if you need to rerun your report.

提交回复
热议问题