在Oracle函数中获取上个月的最后一天

时间:2022-10-16 10:49:14

I need a function in Oracle like this.

我需要像这样的Oracle中的函数。

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 .

我怎样才能做到这一点?顺便说一下,我从不使用Oracle。

3 个解决方案

#1


28  

SELECT LAST_DAY(ADD_MONTHS(yourdate,-1))

#2


8  

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.

我可能仍然建议使用last_day(add_months(xxx,-1))但只是显示替代方案。

#3


2  

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

从dual中选择LAST_DAY(ADD_MONTHS(sysdate,-1));

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

根据你的喜好格式化结果日期(我将为你留下那个;)

#1


28  

SELECT LAST_DAY(ADD_MONTHS(yourdate,-1))

#2


8  

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.

我可能仍然建议使用last_day(add_months(xxx,-1))但只是显示替代方案。

#3


2  

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

从dual中选择LAST_DAY(ADD_MONTHS(sysdate,-1));

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

根据你的喜好格式化结果日期(我将为你留下那个;)