Getting the last day of the previous month in an Oracle function

I need a function in Oracle like this.

When I give the parameter a simple date. Then the function should get me on the 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 this? By the way, I never use Oracle .

+6
function database oracle date-arithmetic
source share
3 answers
 SELECT LAST_DAY(ADD_MONTHS(yourdate,-1)) 
+27
source share

As an alternative to other answers, here you can also find the last day of the previous month, having received the day until 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 show an alternative.

+7
source share

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

format of the final date as you like (I will leave it for you;)

+2
source share

All Articles