Oracle date function for the previous month

后端 未结 6 2122
醉梦人生
醉梦人生 2021-02-18 17:25

I have the query below where the date is hard-coded. My objective is to remove the harcoded date; the query should pull the data for the previous month when it runs.

<         


        
相关标签:
6条回答
  • 2021-02-18 17:46

    Data for last month-

    select count(distinct switch_id)
      from xx_new.xx_cti_call_details@appsread.prd.com
     where dealer_name =  'XXXX'
       and to_char(CREATION_DATE,'MMYYYY') = to_char(add_months(trunc(sysdate),-1),'MMYYYY');
    
    0 讨论(0)
  • 2021-02-18 17:59

    I believe this would also work:

    select count(distinct switch_id)   
      from xx_new.xx_cti_call_details@appsread.prd.com  
     where 
       dealer_name =  'XXXX'    
       and (creation_date BETWEEN add_months(trunc(sysdate,'mm'),-1) and  trunc(sysdate, 'mm'))
    

    It has the advantage of using BETWEEN which is the way the OP used his date selection criteria.

    0 讨论(0)
  • 2021-02-18 18:01

    It is working with me in Oracle sql developer

        SELECT add_months(trunc(sysdate,'mm'), -1),
               last_day(add_months(trunc(sysdate,'mm'), -1)) 
        FROM dual
    
    0 讨论(0)
  • 2021-02-18 18:06

    The trunc() function truncates a date to the specified time period; so trunc(sysdate,'mm') would return the beginning of the current month. You can then use the add_months() function to get the beginning of the previous month, something like this:

    select count(distinct switch_id)   
      from xx_new.xx_cti_call_details@appsread.prd.com  
     where dealer_name =  'XXXX'    
       and creation_date >= add_months(trunc(sysdate,'mm'),-1) 
       and creation_date < trunc(sysdate, 'mm')
    

    As a little side not you're not explicitly converting to a date in your original query. Always do this, either using a date literal, e.g. DATE 2012-08-31, or the to_date() function, for example to_date('2012-08-31','YYYY-MM-DD'). If you don't then you are bound to get this wrong at some point.

    You would not use sysdate - 15 as this would provide the date 15 days before the current date, which does not seem to be what you are after. It would also include a time component as you are not using trunc().


    Just as a little demonstration of what trunc(<date>,'mm') does:

    select sysdate
         , case when trunc(sysdate,'mm') > to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
                 then 1 end as gt
         , case when trunc(sysdate,'mm') < to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
                 then 1 end as lt
         , case when trunc(sysdate,'mm') = to_date('20120901 00:00:00','yyyymmdd hh24:mi:ss')
                 then 1 end as eq
      from dual
           ;
    
    SYSDATE                   GT         LT         EQ
    ----------------- ---------- ---------- ----------
    20120911 19:58:51                                1
    
    0 讨论(0)
  • 2021-02-18 18:06

    Getting last nth months data retrieve

    SELECT * FROM TABLE_NAME 
    WHERE DATE_COLUMN BETWEEN '&STARTDATE' AND '&ENDDATE'; 
    
    0 讨论(0)
  • 2021-02-18 18:09

    Modifying Ben's query little bit,

     select count(distinct switch_id)   
      from xx_new.xx_cti_call_details@appsread.prd.com  
     where dealer_name =  'XXXX'    
       and creation_date between add_months(trunc(sysdate,'mm'),-1) and last_day(add_months(trunc(sysdate,'mm'),-1))
    
    0 讨论(0)
提交回复
热议问题