Getting Last Day of Previous Month in Oracle Function
Asked Answered
G

4

10

I need a function in Oracle like this.

When i giving a parameter a simple date. Then function should getting me last day of the previous month.

Example:

FunctionName(10.02.2011) Result should be 31.01.2011

FunctionName(21.03.2011) Result should be 28.02.2011

FunctionName(31.07.2011) Result should be 30.06.2011 (Even date is last day of month)

How can i do that? By the way, i never use Oracle .

Goody answered 10/2, 2011 at 12:43 Comment(0)
P
37
SELECT LAST_DAY(ADD_MONTHS(yourdate,-1))
Paton answered 10/2, 2011 at 12:47 Comment(1)
These functions have been part of standard Oracle for a couple of decades.Esquibel
O
9

As an alternative to the other answers here, you can also find the last day of the previous month by getting the day before the first day of this month

SELECT trunc(your_date, 'MM')-1 as new_date from your_table

I would probably still recommend using last_day(add_months(xxx,-1)) but just showing an alternative.

Odoacer answered 10/2, 2011 at 14:0 Comment(0)
G
4

select LAST_DAY(ADD_MONTHS(sysdate, -1)) from dual;

format resulting date as you like (I'll leave that one for you ;)

Gallbladder answered 10/2, 2011 at 12:47 Comment(0)
C
0

With this you can also get the last BUSINESS DAY of the previous month, or any other month changing the -1.

SELECT (trunc(LAST_DAY(ADD_MONTHS(sysdate,-1)), 'iw') + 4) from dual
Coomb answered 14/9, 2018 at 17:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.