How to get difference from two timestamp in DB2?

2019-02-28 08:29发布

问题:

i have a requirement in which i have to get time difference of two timestamp in hours and than later find an average of the hours.

i am using below query to find difference of two timestamp but it does not give exact result it gives approx result. do we have any other solution to achieve the same.My two time stamps are as( LAST_MODIFIED_DATETIME - 2016-11-30 15:39:01.131 CREATE_DATETIME - 2016-07-01 17:25:52.375)

select timestampdiff(8, char(LAST_MODIFIED_DATETIME-CREATE_DATETIME)) as total_time from test_table where name='some name';

回答1:

It's easy, by converting the timestamp to hours as follows:

(24*DAYS(last_modified_datetime)+MIDNIGHT_SECONDS(last_modified_datetime)/3600)
-
(24*DAYS(create_datetime)+MIDNIGHT_SECONDS(create_datetime)/3600)


标签: sql db2