Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Date - How to add years to date

I have a date field

DATE = 10/10/2010 

sum = 4 (this are number of years by calculation)

is there a way to add four years to 10/10/2010 and make it 10/10/2014?

like image 318
kalls Avatar asked Feb 16 '12 14:02

kalls


People also ask

Is there a year function in Oracle?

The YEAR function returns the year part of a value. The argument must be a date, timestamp, or a valid character string representation of a date or timestamp. The result of the function is an integer between 1 and 9 999.

Can we add two dates in Oracle?

Adding two dates is meaningless. You can add an interval to a date.

What does ADD_MONTHS do in Oracle?

ADD_MONTHS returns the date date plus integer months. The date argument can be a datetime value or any value that can be implicitly converted to DATE . The integer argument can be an integer or any value that can be implicitly converted to an integer.

Is year a data type in Oracle?

Oracle stores year data with the century information. For example, the Oracle database stores 1996 or 2001, and not just 96 or 01. The DATE datatype always stores a four-digit year internally, and all other dates stored internally in the database have four digit years.


2 Answers

Use 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.

So add_months(to_date('28-feb-2011'),12) will return 29-feb-2012 as a result.

like image 21
Johan Avatar answered Sep 20 '22 18:09

Johan


Try adding months (12 * number of years) instead. Like this-

add_months(date'2010-10-10', 48) 
like image 93
sgowd Avatar answered Sep 19 '22 18:09

sgowd