Click here to Skip to main content
15,908,445 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello, i have number of tasks assigned to sales person.the task is flow in sequence one after another. the sales manager have watch on that task.

EmpId OrderId   TaskName           TaskAssignedDate 
1        1      TakeOrder          11 Nov 2015 10:05:07
2        1      ProcessOrder       11 Nov 2015 10:07:07
3        1      PackOrder          12 Nov 2015 11:44:07
4        1      CourierOrder       13 Nov 2015 13:15:07
5        1      DeliverOrder       14 Nov 2015 15:32:07



this are the records in my table ... how can i get the time between any two tasks.
say from TakeOrder to CourierOrder OR PackOrder to DeliverOrder
Posted
Updated 13-Nov-15 19:08pm
v3

DECLARE @TakeOrder DATETIME;
DECLARE @ProcessOrder  DATETIME;
 
-- Time of the ARTICLE created
SET @TakeOrder = '11 Nov 2015 10:05:07';
 
-- Simulation of NOW datetime
-- (in real world you would probably use GETDATE())
SET @ProcessOrder  = '11 Nov 2015 10:07:07';
 
-- Created 9 days ago.
SELECT 'Diff ' + CAST(DATEDIFF(day, @TakeOrder, @ProcessOrder ) AS NVARCHAR(50)) + ' days ago.' as diff_by_day;
 
-- Created 1 weeks, 2 days, 3 hours, 25 minutes and 20 seconds ago.
SELECT 'Diff '
    + CAST(DATEDIFF(second, @TakeOrder, @ProcessOrder ) / 60 / 60 / 24 / 7 AS NVARCHAR(50)) + ' weeks, '
    + CAST(DATEDIFF(second, @TakeOrder, @ProcessOrder ) / 60 / 60 / 24 % 7 AS NVARCHAR(50)) + ' days, '
    + CAST(DATEDIFF(second, @TakeOrder, @ProcessOrder ) / 60 / 60 % 24  AS NVARCHAR(50)) + ' hours, '
    + CAST(DATEDIFF(second, @TakeOrder, @ProcessOrder ) / 60 % 60 AS NVARCHAR(50)) + ' minutes and '
    + CAST(DATEDIFF(second, @TakeOrder, @ProcessOrder ) % 60 AS NVARCHAR(50)) + ' seconds ago.' as Difference_by_hole;
 
Share this answer
 
Try it by using DATEDIFF(datepart,startdate,enddate) function.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900