SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


DATEDIFF in hours and minits In Decimal Format


DATEDIFF in hours and minits In Decimal Format

Author
Message
skb 44459
skb 44459
SSC Eights!
SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)SSC Eights! (977 reputation)

Group: General Forum Members
Points: 977 Visits: 297
Calculating Difference between ClockIn and ClockOut by Job.
Table : JobTran

Job# ClockIn ClockOut
---------------------------------------------------------------------
Job1 2013-10-03 06:17:34.387 NULL
Job1 2013-10-03 05:17:34.387 2013-10-03 08:10:34.387
Job2 2013-10-03 09:17:34.387 2013-10-03 10:05:34.387
Job2 2013-10-03 09:17:34.387 NULL

if the ClockOut is NULL I would use GETDATE() to calculate the difference between ClockIn and ClockOut

Assuming GETDATE() is 2013-10-03 11:15:34.387

Job1 - 7.92 total hours (4.96 from 1st record, 2.96 from second record )
Job2 - 2.76 total hours (0.8 from 1st record, 1.96 from second record )
Sean Pearce
Sean Pearce
SSCarpal Tunnel
SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)SSCarpal Tunnel (4.1K reputation)

Group: General Forum Members
Points: 4142 Visits: 3436
CREATE TABLE JobTran
(JobNbr VARCHAR(10),
ClockIn DATETIME NOT NULL,
ClockOut DATETIME NULL);

INSERT INTO JobTran (JobNbr, ClockIn, ClockOut)
VALUES ('Job1', '2013-10-03 06:17:34.387', NULL),
('Job1', '2013-10-03 05:17:34.387', '2013-10-03 08:10:34.387'),
('Job2', '2013-10-03 09:17:34.387', '2013-10-03 10:05:34.387'),
('Job2', '2013-10-03 09:17:34.387', NULL);



DECLARE @GetDate DATETIME = GETDATE();

/* For your Test */ SELECT @GetDate = '2013-10-03 11:15:34.387';

SELECT
JobNbr,
SUM(DATEDIFF(SECOND, ClockIn, ISNULL(ClockOut, @GetDate)) / 60. / 60.) AS ClockTime
FROM
JobTran
GROUP BY
JobNbr;





The SQL Guy @ blogspot

@SeanPearceSQL

About Me
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search