Oracle Add 1 hour to SQL

I'm just trying to add 1 hour to a value, it's pretty complicated where and why I do it, but basically I just need to request something like this

select DATE_ADD(hh,1,'2014-10-15 03:30:00 pm') from dual 

I keep reading old articles that say use dateAdd or date_add, but I keep getting invalid id errors.

+13
source share
6 answers
 select sysdate + 1/24 from dual; 

sysdate is a function with no arguments that returns a DATE type
+ 1/24 adds 1 hour to the date

 select to_char(to_date('2014-10-15 03:30:00 pm', 'YYYY-MM-DD HH:MI:SS pm') + 1/24, 'YYYY-MM-DD HH:MI:SS pm') from dual; 
+31
source

Use interval:

 select some_date_column + interval '1' hour from your_table; 
+12
source

You can use the INTERVAL type or just add the calculated value of the number - "1" is equal to "1 day".

First way:

 select date_column + INTERVAL '0 01:00:00' DAY TO SECOND from dual; 

second way:

 select date_column + 1/24 from dual; 

The first method is more convenient when you need to add a complex value - for example, "1 day 3 hours 25 minutes 49 seconds." See also: http://www.oracle-base.com/articles/misc/oracle-dates-timestamps-and-intervals.php

You should also remember that the oracle has two types of intervals - DAY TWO and YEAR PER MONTH. For me, one type of interval would be better, but I hope people in the oracle know what they are doing;)

+5
source

Old way :

 SELECT DATE_COLUMN + 1 is adding a day SELECT DATE_COLUMN + N /24 to add hour(s) - N being number of hours SELECT DATE_COLUMN + N /1440 to add minute(s) - N being number of minutes SELECT DATE_COLUMN + N /86400 to add second(s) - N being number of seconds 

Using INTERVAL :

 SELECT DATE_COLUMN + INTERVAL 'N' HOUR or MINUTE or SECOND - N being a number of hours or minutes or seconds. 
+2
source

The calculation is simple

if you want to add 1 hour per day.

every day there is 24 hours, you can add.

 select sysdate + 1/24 from dual; 

if you want to add 1 day

 select sysdate + 24/24 from dual; or select sysdate + 1 from dual; same as for 2, 3 , 4 day 

For a static date, you have the answer below.

+1
source

To add / subtract from DATE , you have 2 options:

Method # 1: The easiest way is to use + and - to add / subtract days, hours, minutes, seconds, etc. From the DATE function, and ADD_MONTHS() to add / subtract months and years from DATE . Why? This is because from days you can get hours and any smaller unit (1 hour = 1/24 days), (1 minute = 1/1440 days), etc. But you cannot get months and years, since it depends on the months and years themselves, therefore, ADD_MONTHS() and no add_years (), because months can be used to get years (1 year = 12 months).

Let's try them:

 SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints current date: 19-OCT-2019 20:42:02 SELECT TO_CHAR((SYSDATE + 1/24), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour: 19-OCT-2019 21:42:02 SELECT TO_CHAR((SYSDATE + 1/1440), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 minute: 19-OCT-2019 20:43:02 SELECT TO_CHAR((SYSDATE + 1/86400), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 second: 19-OCT-2019 20:42:03 -- Same goes for subtraction. SELECT SYSDATE FROM dual; -- prints current date: 19-OCT-19 SELECT ADD_MONTHS(SYSDATE, 1) FROM dual; -- prints date + 1 month: 19-NOV-19 SELECT ADD_MONTHS(SYSDATE, 12) FROM dual; -- prints date + 1 year: 19-OCT-20 SELECT ADD_MONTHS(SYSDATE, -3) FROM dual; -- prints date - 3 months: 19-JUL-19 

Method # 2: Using INTERVAL s, you can easily subtract the interval (duration) from the date. Moreover, you can combine the addition or subtraction of several units at the same time (for example, 5 hours and 6 minutes, etc.) Examples:

 SELECT TO_CHAR(SYSDATE, 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints current date: 19-OCT-2019 21:34:15 SELECT TO_CHAR((SYSDATE + INTERVAL '1' HOUR), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour: 19-OCT-2019 22:34:15 SELECT TO_CHAR((SYSDATE + INTERVAL '1' MINUTE), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 minute: 19-OCT-2019 21:35:15 SELECT TO_CHAR((SYSDATE + INTERVAL '1' SECOND), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 second: 19-OCT-2019 21:34:16 SELECT TO_CHAR((SYSDATE + INTERVAL '01:05:00' HOUR TO SECOND), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 1 hour and 5 minutes: 19-OCT-2019 22:39:15 SELECT TO_CHAR((SYSDATE + INTERVAL '3 01' DAY TO HOUR), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date + 3 days and 1 hour: 22-OCT-2019 22:34:15 SELECT TO_CHAR((SYSDATE - INTERVAL '10-3' YEAR TO MONTH), 'DD-MON-YYYY HH24:MI:SS') FROM dual; -- prints date - 10 years and 3 months: 19-JUL-2009 21:34:15 
0
source

Source: https://habr.com/ru/post/1205280/


All Articles