Thursday, October 11, 2018

AX, SQL SERVER - How to get difference between date and time columns in hours

Suppose this code:
select prodid, fromdate, fromtime, todate, totime from prodroutejob
where
prodid = '18-044906' and
jobtype = 4;
..and this result:
prodid               fromdate                fromtime    todate                  totime
-------------------- ----------------------- ----------- ----------------------- -----------
18-044906            2018-10-08 00:00:00.000 42933       2018-10-08 00:00:00.000 53735
18-044906            2018-10-09 00:00:00.000 31905       2018-10-09 00:00:00.000 39106
18-044906            2018-10-10 00:00:00.000 46033       2018-10-10 00:00:00.000 53234

(3 row(s) affected)
How to get difference between FROM and TO in hours ?

Use this code:
select datediff( hour, [start], [end] ) from
(
select dateadd( second, a.fromtime, a.fromdate ) as [start],
dateadd( second, a.totime, a.todate ) as [end]
from prodroutejob a
where
a.prodid = '18-044906' and
a.jobtype = 4
) a
Output:
diff
-----------
3
2
2

(3 row(s) affected)

No comments:

Post a Comment