Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DateAdd function in pl/sql

Tags:

plsql

As the title suggested , I am looking for a function in pl /sql which does something similar like the DateAdd function. I have been looking and I found the add_months function but I would really like one that is a little more variable since I need to be able to add minutes, hours , days etc.

like image 687
jovany Avatar asked Oct 25 '10 08:10

jovany


2 Answers

It appears there's not many solutions :

PL/SQL allows you to perform arithmetic operations directly on date variables. You may add numbers to a date or subtract numbers from a date. To move a date one day in the future, simply add 1 to the date as shown below:

hire_date + 1

You can even add a fractional value to a date. For example, adding 1/24 to a date adds an hour to the time component of that value. Adding 1/(24*60) adds a single minute to the time component, and so on.

like image 187
MatTheCat Avatar answered Oct 20 '22 16:10

MatTheCat


Besides adding numbers to dates - though it's the simplest way - you can add intervals like that:

date1 := date2 + interval '1' day;
date1 := date2 + interval '2' month;
date1 := date2 + interval '3' year;

It's almost the same but I prefer latter for better readability.

like image 32
andr Avatar answered Oct 20 '22 15:10

andr