Add Day to Timestamp
Asked Answered
W

3

22

How do I add days to a timestamp? If my timestamp is 01-JAN-2011 11-09-05 and I add 2 days, I want 03-JAN-2011 11-09-05.

Weigela answered 26/5, 2011 at 17:51 Comment(0)
B
37
select '01-jan-2011 11-09-05' + interval '2' day
Borneo answered 26/5, 2011 at 17:53 Comment(2)
+1. A more complete Oracle example: SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + INTERVAL '2' DAY FROM dual;Suet
Using interval seems better than using + because I noticed loss of timezone with the latter.Cointreau
S
4

A completely Oracle-centric solution is to simply add 2 to the timestamp value as the default interval is days for Oracle dates/timestamps:

SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + 2
  FROM dual;
Suet answered 26/5, 2011 at 18:36 Comment(2)
a timestamp plus a numeric value yields a date.Robichaux
you may lose the timezone by using +.Cointreau
D
2

In a similar case, I used:

SELECT TO_TIMESTAMP('01-jan-2011 11-09-05','DD-Mon-YYYY HH24-MI-SS') + NUMTODSINTERVAL(2, 'DAY')

Because, othewise, the expression is converted to DATE and precission is lost. See: NUMTODSINTERVAL documentation

Directly answered 17/2, 2022 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.