Solved

sql 2005 query

Posted on 2007-12-03
3
234 Views
Last Modified: 2010-03-19
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
Comment
Question by:romeiovasu
  • 2
3 Comments
 
LVL 8

Expert Comment

by:srafi78
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

by:
srafi78 earned 250 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

by:David Todd
David Todd earned 250 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
	

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
sqlquerystress - To test db performance 8 39
sql help 8 55
SSRS  - Dropdown with Null 3 24
insert wont work in SQL 14 19
Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

805 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question