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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4957224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:03:39  来源:igfitidea点击:

Getting Last Day of Previous Month in Oracle Function

databaseoraclefunctiondate-arithmetic

提问by Soner G?nül

I need a function in Oraclelike 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.

回答by Lamak

SELECT LAST_DAY(ADD_MONTHS(yourdate,-1))

回答by Mike Meyers

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))但只是展示一个替代方案。

回答by tbone

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

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

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

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

回答by LEBRO

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

有了这个,您还可以获得上个月的最后一个工作日,或更改 -1 的任何其他月份。

SELECT (trunc(LAST_DAY(ADD_MONTHS(sysdate,-1)), 'iw') + 4) from dual