I know that it doesnt fully take in everything that you need it to but you can use the DATEDIFF function to workout the number of hours between two dates:
Which version of SQL Server? Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
You can use Datediff but using the Hour will be inaccurate as Datediff will return the difference of 1 hour between 8:59AM and 9:01AM. Use minute instead and divide by 60.
If the dates can span days and you want to know the number of working hours in that date range, you'll need to create a stored procedure to perform the calculations. You may need to modify to meet your needs but this should give you an idea of how to start.
Set @cd=convert(char(11), @sd)
If datepart(dw, @sd) Between 2 and 6
Begin
If datepart(hour,@sd) Between 9 and 17
Begin
Set @WorkHrs=Datediff(minute, @sd, @cd + '17:00:00')/60.
End
Else
If datepart(hour,@cd)>17
Begin
Set @WorkHrs=0
End
Else
Begin
Set @WorkHrs=8
End
End
Else
Begin
Set @WorkHrs=0
End
Set @cd=@cd+1
While @cd<convert(char(11), @ed)
Begin
If datepart(dw,@cd) Between 2 and 6
Begin
Set @WorkHrs = @WorkHrs + 8
End
Set @cd=@cd+1
End
If datepart(dw, @ed) Between 2 and 6
Begin
If datepart(hour,@sd) Between 9 and 17
Begin
Set @WorkHrs=@workhrs+Datediff(minute, @ed, @cd + '17:00:00')/60.
End
Else
If datepart(hour,@cd)>17
Begin
Set @WorkHrs=@workHrs+0
End
Else
Begin
Set @WorkHrs=@WorkHrs+8
End
End
Print @workhours Terry L. Broadbent
FAQ183-874 contains tips for posting questions in these forums.
NOTE: Reference to the FAQ is not directed at any individual.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.