Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
AskSQLTeam
Ask SQLTeam Question
0 Posts |
Posted - 2006-07-12 : 11:18:57
|
| Uriel Bravo writes "Uriel Bravo writes "I am trying to create a list of how many hours counting into a date range (startdate/enddate) without weekend days Saturday/Sunday, a employee work in our system.So if an employee has registered 8 hours by day and the request was created with the datestamp "2006-01-01 00:00:00.000" which is a sunday, and I run the report on "2006-01-15 00:00:00.000" I would get 80 hours (Just the Absolute number of hours by day it means 24 hours by day) because we have day 1,7,8,14 and 15 as weekdays.Any experts out there who can give me a helping hand?"" |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
|
|
barry
Starting Member
14 Posts |
Posted - 2006-07-12 : 17:38:14
|
| This function may help; you could get the number of work days between dates create function workdays_between(@date_1 datetime, @date_2 datetime)RETURNS INTASBEGINDECLARE @startdate datetime, @enddate datetime, @weeks int, @days intIF @date_1 > @date_2BEGINSET @startdate = @date_2SET @enddate = @date_1ENDELSEBEGINSET @startdate = @date_1SET @enddate = @date_2END--SELECT @startdate = dbo.add_workdays(@startdate,0)--SELECT @enddate = dbo.add_workdays(@enddate,0)SELECT @weeks = DATEDIFF(day, @startdate, @enddate) / 7SELECT @days = DATEDIFF(day, @startdate, @enddate) % 7IF (DATEPART(dw, @enddate -1) < DATEPART(dw, @startdate -1))BEGINSELECT @days = @days - 2ENDRETURN (@weeks * 5) + @daysEND |
 |
|
|
|
|
|
|
|