我想找到两个时间戳之间的那一天。查询必须返回Integer值。 我有一个列值和一个固定日期(比如t.movementdate和'2014-07-23 00:00:00.0')。
答案 0 :(得分:2)
你可以试试这个
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);
答案 1 :(得分:0)
好问题。恕我直言Oracle的时间戳算法非常难看。如果您需要天数差异,则可以将TIMESTAMP
转换为DATE
。根据您的需要,您还可以使用TRUNC
删除分钟和小时。获得DATE
后,您可以简单地减去它们以获得天数差异:
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