Search code examples
oracleoracle11gdate-arithmetic

How can we achieve last month record in oracle


I am new to oracle and I am writing a query where I have to fetch last month My query:

 select to_char(sysdate,'MON') from dual  

But it is giving me
AUG which is current month.
I want the output as JUL which is last month

How can we achieve last month record.


Solution

  • select to_char(ADD_MONTHS (SYSDATE, -1),'MON') from dual 
    
    --Move ahead one month: 
    ADD_MONTHS (SYSDATE, 1);
    
    --Move backward 1 month: 
    ADD_MONTHS (SYSDATE, -1);  
    

    http://www.oracle.com/technetwork/issue-archive/2012/12-jan/o12plsql-1408561.html

    Oracle Database offers several built-in functions for shifting a date by the requested amount or finding a date:

     ADD_MONTHS—adds the specified number of months to or subtracts it from a date (or a timestamp) 
    NEXT_DAY—returns the date of the first weekday named in the call to the function 
    LAST_DAY—returns the date of the last day of the month of the specified date