Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent function for DATEADD() in Oracle

Tags:

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 equivalent function in Oracle?

like image 896
Geethu Avatar asked Jun 25 '14 09:06

Geethu


People also ask

What is the function of Dateadd?

You can use the DateAdd function to add or subtract a specified time interval from a date. For example, you can use DateAdd to calculate a date 30 days from today or a time 45 minutes from now. To add days to date, you can use Day of Year ("y"), Day ("d"), or Weekday ("w").

What is ADD_MONTHS function in SQL?

The ADD_MONTHS function takes a DATETIME or DATE expression as its first argument, and requires a second integer argument, specifying the number of months to add to the first argument value. The second argument can be positive or negative.

What is ADD months function 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.


2 Answers

Method1: ADD_MONTHS

ADD_MONTHS(SYSDATE, -6)

Method 2: Interval

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 Date datatype as input.

like image 107
Maheswaran Ravisankar Avatar answered Sep 19 '22 15:09

Maheswaran Ravisankar


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

like image 26
Papa Stahl Avatar answered Sep 20 '22 15:09

Papa Stahl