x
Solved

# sql 2005 query

Posted on 2007-12-03
Medium Priority
273 Views
HI all,
i have table with
employeeid number
timein datetime
timout datetime

how can i calculate total punched hours. how can i calculate it timeout is null

the sample data i am giving.
empid   storeid      punchin                  punchout
2      1      2007-07-18 18:44:26.897      NULL
95      1      2007-08-22 17:20:04.373      NULL
2      5      2007-08-21 08:00:00.000      2007-08-21 10:00:00.000
2      5      2007-08-21 20:43:17.387      2007-08-22 11:08:40.343
59      7      2007-10-02 15:10:00.000      2007-10-02 18:56:44.973
62      7      2007-09-08 09:15:00.000      2007-09-08 19:30:00.000
62      7      2007-09-17 09:21:00.000      2007-09-17 17:00:00.000
0
Question by:romeiovasu
• 2

LVL 8

Expert Comment

ID: 20399436
Select empid, storeid, sum(hours) from
(Select empid, storeid, Case when punchout is null then 0 else
DateDiss(hours , punchin, punchout) end as hours from employee)A
group by empid, storeid
0

LVL 8

Accepted Solution

srafi78 earned 1000 total points
ID: 20399445
Oops typo
Select empid, storeid, sum(hours) from
(Select empid, storeid, Case when punchout is null then 0 else
DateDiff(hour , punchin, punchout) end as hours from employee)A
group by empid, storeid
0

LVL 35

Assisted Solution

David Todd earned 1000 total points
ID: 20399701
Hi,

I'd suggest that instead of taking the datediff by hours that you do it in minutes else get only whole hours. And the amount of hours isn't whole hours as the code snippet shows.

Of course if you want hours you'll have to divide the minutes by 60, but you know that.

Cheers
David
``````use tempdb
go

declare @StartDate datetime
declare @EndDate datetime

select
@StartDate = '2007-12-04 11:55:00'
, @EndDate = '2007-12-04 12:05:00'

select
datediff( hour, @StartDate, @EndDate ) as hours
, datediff( minute, @StartDate, @EndDate ) as mins

``````
0

## Featured Post

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.