Solved

sql 2005 query

Posted on 2007-12-03
3
213 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now