Reputation: 355
I want to find the day between two timestamps. The query must return an Integer value. I have a column value and a fixed date (like t.movementdate and '2014-07-23 00:00:00.0').
Upvotes: 2
Views: 153
Reputation: 455
You can try this
CREATE TABLE t (movementdate TIMESTAMP);
INSERT INTO t VALUES (TIMESTAMP '1014-07-21 03:23:02.0');
INSERT INTO t VALUES (TIMESTAMP '2014-07-22 10:54:02.0');
select actual_diff,extract (day from actual_diff)+
extract (hour from actual_diff)/24
+extract (minute from actual_diff)/(60*24)
+extract (second from actual_diff)/(60*60*24)
diff_in_days
from (select systimestamp- movementdate as actual_diff from t);
Upvotes: 2
Reputation: 8361
Good question. IMHO Oracle's timestamp arithmetic is quite ugly. If you need the difference in days, you can convert the TIMESTAMP
to a DATE
. Depending on your needs, you can also drop the minutes and hours with TRUNC
. Once you have DATE
s, you can simply substract them to get the difference in days:
CREATE TABLE t (movementdate TIMESTAMP);
INSERT INTO t VALUES (TIMESTAMP '2014-07-21 03:23:02.0');
INSERT INTO t VALUES (TIMESTAMP '2014-07-22 10:54:02.0');
SELECT t.movementdate, DATE '2014-07-23' - trunc(t.movementdate) as daydiff FROM t;
MOVEMENTDATE DAYDIFF
---------------------------- ----
21.07.2014 03:23:02,000000000 2
22.07.2014 10:54:02,000000000 1
Upvotes: 0