SQL Oracle 中 DATEADD() 的等效函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24405297/
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-01 02:06:58  来源:igfitidea点击:

Equivalent function for DATEADD() in Oracle

sqloracle

提问by Geethu

I have to get a date that is 6 months from the system date in Oracle.
And I have to get it by running an open-query from SQL. DATEADD(MONTH,-6, GETDATE())function serves the purpose in SQL.

Does the function DATEADD(MONTH,-6, GETDATE())in SQL have an equivalentfunction in Oracle?

我必须得到一个距 Oracle 系统日期 6 个月的日期。
我必须通过从 SQL 运行开放查询来获取它。DATEADD(MONTH,-6, GETDATE())函数在 SQL 中起到作用。

请问功能DATEADD(MONTH,-6, GETDATE())在SQL有一个equivalent在Oracle中的功能?

回答by Maheswaran Ravisankar

Method1:ADD_MONTHS

方法 1:ADD_MONTHS

ADD_MONTHS(SYSDATE, -6)

ADD_MONTHS(SYSDATE, -6)

Method 2:Interval

方法二:间隔

SYSDATE - interval '6' month

SYSDATE - interval '6' month

Note:if you want to do the operations from start of the current month always, TRUNC(SYSDATE,'MONTH')would give that. And it expects a Datedatatype as input.

注意:如果您想始终从当月开始进行操作,请TRUNC(SYSDATE,'MONTH')给出。它需要一个Date数据类型作为输入。

回答by Papa Stahl

Not my answer:

不是我的回答

I wasn't too happy with the answers above and some additional searching yielded this :

我对上面的答案不太满意,一些额外的搜索产生了这个:

SELECT SYSDATE AS current_date,

       SYSDATE + 1 AS plus_1_day,

       SYSDATE + 1/24 AS plus_1_hours,

       SYSDATE + 1/24/60 AS plus_1_minutes,

       SYSDATE + 1/24/60/60 AS plus_1_seconds

FROM   dual;

which I found very helpful. From http://sqlbisam.blogspot.com/2014/01/add-date-interval-to-date-or-dateadd.html

我发现这很有帮助。来自http://sqlbisam.blogspot.com/2014/01/add-date-interval-to-date-or-dateadd.html

回答by hashbrown

Equivalent will be

等效将是

ADD_MONTHS( SYSDATE, -6 )

回答by cluelessgumshoe

--ORACLE SQL EXAMPLE
SELECT
SYSDATE
,TO_DATE(SUBSTR(LAST_DAY(ADD_MONTHS(SYSDATE, -1)),1,10),'YYYY-MM-DD')
FROM DUAL