Home » SQL & PL/SQL » SQL & PL/SQL » How to get first and last day for 13 month back
How to get first and last day for 13 month back [message #645006] Mon, 23 November 2015 09:06 Go to next message
blyzz
Messages: 10
Registered: October 2015
Junior Member
I need the function to get first and last day of the13 month back from the current month in Oracle.
Ex if the current month is Nov, I want first and last day for Oct 2014 and next month, it should be first and last day of Nov 2014.

How can I do that?

Thanks,
Blyzz
Re: How to get first and last day for 13 month back [message #645007 is a reply to message #645006] Mon, 23 November 2015 09:09 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3312
Registered: January 2010
Location: Connecticut, USA
Senior Member
ADD_MONTHS + LAST_DAY

SY.
Re: How to get first and last day for 13 month back [message #645008 is a reply to message #645007] Mon, 23 November 2015 09:09 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
http://www.orafaq.com/forum/mv/msg/198886/643534/#msg_643534
Re: How to get first and last day for 13 month back [message #645009 is a reply to message #645006] Mon, 23 November 2015 09:11 Go to previous messageGo to next message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

SQL> select trunc(add_months(sysdate,-14+level),'month') first_d,
  2         last_day(trunc(add_months(sysdate,-14+level),'month')) last_d
  3  from dual
  4  connect by level <= 14
  5  /
FIRST_D     LAST_D
----------- -----------
01-OCT-2014 31-OCT-2014
01-NOV-2014 30-NOV-2014
01-DEC-2014 31-DEC-2014
01-JAN-2015 31-JAN-2015
01-FEB-2015 28-FEB-2015
01-MAR-2015 31-MAR-2015
01-APR-2015 30-APR-2015
01-MAY-2015 31-MAY-2015
01-JUN-2015 30-JUN-2015
01-JUL-2015 31-JUL-2015
01-AUG-2015 31-AUG-2015
01-SEP-2015 30-SEP-2015
01-OCT-2015 31-OCT-2015
01-NOV-2015 30-NOV-2015

Re: How to get first and last day for 13 month back [message #645010 is a reply to message #645009] Mon, 23 November 2015 09:18 Go to previous messageGo to next message
blyzz
Messages: 10
Registered: October 2015
Junior Member
Thank you
Re: How to get first and last day for 13 month back [message #645011 is a reply to message #645007] Mon, 23 November 2015 09:30 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3312
Registered: January 2010
Location: Connecticut, USA
Senior Member
Actually, no need for LAST_DAY:

SQL> select  trunc(add_months(sysdate,-14+level),'month') first_d,
  2          trunc(add_months(sysdate,-13+level),'month') - 1 last_d
  3    from dual
  4  connect by level <= 14
  5  /

FIRST_D             LAST_D
------------------- -------------------
10/01/2014 00:00:00 10/31/2014 00:00:00
11/01/2014 00:00:00 11/30/2014 00:00:00
12/01/2014 00:00:00 12/31/2014 00:00:00
01/01/2015 00:00:00 01/31/2015 00:00:00
02/01/2015 00:00:00 02/28/2015 00:00:00
03/01/2015 00:00:00 03/31/2015 00:00:00
04/01/2015 00:00:00 04/30/2015 00:00:00
05/01/2015 00:00:00 05/31/2015 00:00:00
06/01/2015 00:00:00 06/30/2015 00:00:00
07/01/2015 00:00:00 07/31/2015 00:00:00
08/01/2015 00:00:00 08/31/2015 00:00:00

FIRST_D             LAST_D
------------------- -------------------
09/01/2015 00:00:00 09/30/2015 00:00:00
10/01/2015 00:00:00 10/31/2015 00:00:00
11/01/2015 00:00:00 11/30/2015 00:00:00

14 rows selected.

SQL> 


SY.
Re: How to get first and last day for 13 month back [message #645013 is a reply to message #645011] Mon, 23 November 2015 10:21 Go to previous message
Michel Cadot
Messages: 68776
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Was just to illustrate a useful date function. Smile

Previous Topic: MERGE: DELETE CLAUSE definition explanation.
Next Topic: Selecting/Inserting Missing values
Goto Forum:
  


Current Time: Mon Jul 06 16:18:08 CDT 2026