oracle 将月份添加到日期 SQL

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

adding months to a date SQL

sqloracledate-arithmetic

提问by Nidhin_toms

I am trying to add months to an existing date in SQL. The new column displayed will have a followup column instead of a days column. Im getting an error in the select statement.can u help?

我正在尝试将月份添加到 SQL 中的现有日期。显示的新列将有一个后续列而不是天数列。我在选择语句中遇到错误。你能帮忙吗?

Create table auctions(
item varchar2(50),
datebought date,
datesold date,
days number
);
Insert into auctions values (‘Radio','12-MAY-2001','21-MAY-2001',9);
Select item,datebought,datesold,ADD MONTHS(datesold,3)”followup” from auctions;

回答by a_horse_with_no_name

Your usage of the add_months() function is incorrect. It's not two words, it's just one (with an underscore)

您对 add_months() 函数的使用不正确。这不是两个词,它只是一个(带下划线)

add_months(datesold, 1)

note the underscore _between ADDand MONTHS. It's function call not an operator.

注意下划线_之间ADDMONTHS。它是函数调用而不是运算符。

Alternatively you could use:

或者,您可以使用:

datesold + INTERVAL '1' month

Although it's worth noting that the arithmetics with intervals is limited (if not broken) because it simply "increments" the month value of the date value. That can lead to invalid dates (e.g. from January to February). Although this is documented behaviour (see below links) I consider this a bug (the SQL standard requires those operations to "Arithmetic obey the natural rules associated with dates and times and yield valid datetime or interval results according to the Gregorian calendar")

尽管值得注意的是带有间隔的算术是有限的(如果没有被破坏),因为它只是“增加”日期值的月份值。这可能导致无效日期(例如从一月到二月)。尽管这是记录在案的行为(请参阅下面的链接),但我认为这是一个错误(SQL 标准要求这些操作“算术遵守与日期和时间相关的自然规则,并根据公历产生有效的日期时间或间隔结果”)

See the manual for details:
http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions011.htm#i76717
http://docs.oracle.com/cd/E11882_01/server.112/e26088/sql_elements001.htm#i48042

有关详细信息,请参阅手册:
http: //docs.oracle.com/cd/E11882_01/server.112/e26088/functions011.htm#i76717
http://docs.oracle.com/cd/E11882_01/server.112/e26088 /sql_elements001.htm#i48042

Another thing:

另一件事:

I am trying to add months to an existing date in SQL.

我正在尝试将月份添加到 SQL 中的现有日期。

Then why are you using an INSERTstatement? To change the data of existing rows you should use UPDATE. So it seems what you are really after is something like this:

那你为什么要使用INSERT语句呢?要更改现有行的数据,您应该使用UPDATE. 所以看起来你真正想要的是这样的:

update auctions
   set datesold = add_months(datesold, 1)
where item = 'Radio';

回答by Ben Graham

Your SQL has typographical quotation marks, not standard ones. E.g. 'is not the same as '. Instead of delimiting a string value, those quotes become part ofthe value, at least for the particular SQL I have here to test with.

您的 SQL 具有印刷引号,而不是标准引号。例如''. 这些引号不是分隔字符串值,而是成为值的一部分,至少对于我在这里要测试的特定 SQL。

If this doesn't fix your problem, try posting the error you're getting in your question. Magical debugging isn't possible.

如果这不能解决您的问题,请尝试发布您在问题中遇到的错误。神奇的调试是不可能的。

回答by Salman

This can be used to add months to a date in SQL:

这可用于在 SQL 中将月份添加到日期:

select DATEADD(mm,1,getdate())

This might be a useful link.

这可能是一个有用的链接