SQL(DB2)使用Timestampdiff和Cast在两个日期之间的时差

时间:2018-11-07 00:00:08

标签: sql casting db2 difference timestampdiff

如果我有两列的起始日期和结束日期,如何使用TIMESTAMPDIFF和CAST运算符找到它们之间的差异。

第1列的开始日期:2001-07-11-16.51.40.000000
结束日期为Column2的日期:2001-07-12-09.21.54.000000

输出:75301 7h 53min 01sec 7.88

我尝试这样的事情

SELECT START_DATE, END_DATE
CAST(TIMESTAMPDIFF(HOUR, START_DATE, END_DATE)) AS TIME
FROM TABLE;

1 个答案:

答案 0 :(得分:1)

您可以使用HOURS_BETWEEN()https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.5.0/com.ibm.db2.luw.sql.ref.doc/doc/r0061478.html在Db2中找到两个日期或时间戳之间的小时数,因此请尝试

VALUES HOURS_BETWEEN(START_DATE, END_DATE)

例如

VALUES HOURS_BETWEEN(current timestamp, current_date)"

1          
-----------
         23

  1 record(s) selected.