Oracle 日期 - 如何添加至今的年份
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9313234/
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
Oracle Date - How to add years to date
提问by kalls
I have a date field
我有一个日期字段
DATE = 10/10/2010
sum = 4 (this are number of years by calculation)
sum = 4(这是通过计算得出的年数)
is there a way to add four years to 10/10/2010 and make it 10/10/2014?
有没有办法将 4 年添加到 10/10/2010 并使其成为 10/10/2014?
回答by sgowd
Try adding months (12 * number of years) instead. Like this-
尝试添加月数(12 * 年数)。像这样-
add_months(date'2010-10-10', 48)
回答by Johan
Use add_months
用 add_months
Example:
例子:
SELECT add_months( to_date('10-OCT-2010'), 48 ) FROM DUAL;
Warning
add_months, returns the last day of the resulting month if you input the last day of a month to begin with.
警告
add_months,如果您输入一个月的最后一天开始,则返回结果月份的最后一天。
So add_months(to_date('28-feb-2011'),12)
will return 29-feb-2012 as a result.
因此add_months(to_date('28-feb-2011'),12)
将返回 29-feb-2012。
回答by Dan
I believe you could use the ADD_MONTHS()
function. 4 years is 48 months, so:
我相信你可以使用这个ADD_MONTHS()
功能。4 年是 48 个月,所以:
add_months(DATE,48)
Here is some information on using the function:
以下是有关使用该功能的一些信息:
http://www.techonthenet.com/oracle/functions/add_months.php
http://www.techonthenet.com/oracle/functions/add_months.php
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1157035034361
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1157035034361
回答by Black
I am not sure, if I understood Your question correctly, but
我不确定,如果我正确理解你的问题,但是
select add_months(someDate, numberOfYears * 12) from dual
might do the trick
可能会成功
回答by Grimmy
回答by SriniV
One more option apart from ADD_MONTHS
除了 ADD_MONTHS 之外的另一种选择
SELECT
SYSDATE,
SYSDATE
+ TO_YMINTERVAL ( '1-0' )
FROM
DUAL;
SYSDATE SYSDATE+TO_YMINTERVAL('1-0')
--------- ----------------------------
29-OCT-13 29-OCT-14
1 row selected.
SELECT
SYSDATE,
SYSDATE
+ TO_YMINTERVAL ( '2-0' )
FROM
DUAL;
SYSDATE SYSDATE+TO_YMINTERVAL('2-0')
--------- ----------------------------
29-OCT-13 29-OCT-15
1 row selected.
SELECT
TO_DATE ( '29-FEB-2004',
'DD-MON-YYYY' )
+ TO_YMINTERVAL ( '1-0' )
FROM
DUAL
*
Error at line 4
ORA-01839: date not valid for month specified
But the last one is illegal since there is no 29th day of February in 2005, hence it fails on leap year cases (Feb 29)
但是最后一个是非法的,因为 2005 年没有 2 月 29 日,因此它在闰年情况下失败(2 月 29 日)
Read the documentationfor the same
阅读相同的文档
回答by Renato Albuquerque
SELECT TO_CHAR(SYSDATE,'YYYY')-2 ANO FROM DUAL