Showing posts with label sql server - datediff(). Show all posts
Showing posts with label sql server - datediff(). Show all posts

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)

Monday, December 11, 2017

SQL SERVER - How get difference between dates (here in days)

Here is example difference between two dates in days (first parameter), but you can use for example hour or month, etc.
select salesid, linenum, itemid, qtyordered, createddatetime, confirmeddlv, 
datediff( day, createddatetime, confirmeddlv ) as diffdays 
from salesline
where
confirmeddlv > '2017/12/01' and
datediff( day, createddatetime, confirmeddlv )  <= 7
Output:
salesid              createddatetime         confirmeddlv            diffdays
-------------------- ----------------------- ----------------------- -----------
PO171475             2017-12-08 10:51:45.000 2017-12-10 00:00:00.000 2
PO171475             2017-12-08 10:52:03.000 2017-12-10 00:00:00.000 2
PO171675             2017-11-27 19:58:26.000 2017-12-04 00:00:00.000 7
...