0

I had a column with dates, in an Select statement, and i want to add 2 h to each row in this Select.

Any advice?

Prisoner
  • 1,839
  • 2
  • 22
  • 38
BOB
  • 700
  • 2
  • 16
  • 35

3 Answers3

5

I prefer using interval for this:

select deliverydate + interval '2' hour
from the_table;

The above is standard SQL and works well in Oracle.

More details in the manual: https://docs.oracle.com/database/121/SQLRF/sql_elements003.htm#SQLRF00221

2

MS Access Update Query:

UPDATE Table1 AS t2 INNER JOIN Table1 ON t2.ID = Table1.ID 
SET Table1.DeliveryDate = DateAdd("h",2,[t2].[DeliveryDate]);

Sql Server Update Query:

UPDATE Table1 AS t2 INNER JOIN Table1 ON t2.ID = Table1.ID 
SET Table1.DeliveryDate = DateAdd("hh",2,[t2].[DeliveryDate]);

MS Access Select Query:

SELECT DateAdd("h",2,Table1.DeliveryDate) AS NewDateTime
FROM Table1;

Sql Server Select Query:

SELECT DateAdd("hh",2,Table1.DeliveryDate) AS NewDateTime
FROM Table1;

Oracle Select Query:

SELECT (Table1.DeliveryDate + 2/24) AS NewDateTime
FROM Table1;
Prisoner
  • 1,839
  • 2
  • 22
  • 38
  • i don't want to update my table – BOB Nov 07 '16 at 06:51
  • See my updated answer. You just need to create a calculated column: In this example : `DateAdd("hh",2,[OldDate]) AS NewDateTime` `NewDateTime` is the field alias/name for a field that is 2 hours greater than the `[OldDate]` fields. You may need to create some additional logic to handle Nulls. –  Nov 07 '16 at 06:59
  • it doesn't work for me, i get error , "DateAdd": invalid identifier – BOB Nov 07 '16 at 06:59
  • I didn't see that you are using Oracle. I update my answer. –  Nov 07 '16 at 07:07
  • Thank you, it works also with your last query. But unfortunately i can't accept 2 answers – BOB Nov 07 '16 at 07:25
  • No worries. Happy Coding!! –  Nov 07 '16 at 07:30
1

enter image description here

CREATE TABLE testtime (my_date datetime);
  INSERT INTo testtime VALUES (GETDATE());

this prints the actual date

  select * from testtime

this prints time + 2 hrs

  select DATEADD(HOUR, 2, my_date) from testtime
Aravind Pillai
  • 739
  • 7
  • 21