簡體   English   中英

比較SQL for DB2中的日期和時間

[英]Compare date and time in SQL for DB2

我需要比較2個字段的日期和時間,看看它是否大於24小時,但是還有另一個字段的值小於比較日期的24小時。 例如 ,

  • 創建日期-18/7/2019 11:15
  • 目標日期-19/07/2019 11:16 AM

這里有24小時的間隔

  • 實際日期-19/07/2019 10:45 AM

實際日期少於上述24小時的間隔。 因此,查詢應返回這樣的記錄,這些記錄的實際日期小於創建日期和24小時的目標日期間隔。 這里,所有字段在DB2數據庫中都是DATETIME數據類型。

目前尚不清楚您真正嘗試做什么。
Actual date is less than above said gap of 24 hours
日期如何與間隔(或持續時間)相提並論?
我們是否應該理解為:
Gap between actual and created is less than gap between target and created
如果是這樣,請嘗試以下操作:

select *
from 
(
select create, target, actual
, timestampdiff(8, char(target - create)) gap_target
, timestampdiff(8, char(actual - create)) gap_actual
from table(values 
  (timestamp('2019-07-18-11.15.00'), timestamp('2019-07-19-11.16.00'), timestamp('2019-07-19-10.45.00'))
) t (create, target, actual)
) 
where 
gap_target >= 24 and gap_actual < 24
-- or some another expression like:
-- gap_actual < gap_target
;  

CREATE              TARGET              ACTUAL              GAP_TARGET GAP_ACTUAL
------------------- ------------------- ------------------- ---------- ----------
2019-07-18 11:15:00 2019-07-19 11:16:00 2019-07-19 10:45:00         24         23

這是你想要的嗎?
如果不是,則提供一些輸入記錄和所需的准確結果。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM