473,418 Members | 2,033 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,418 software developers and data experts.

SQL Business Hours Calculation

Hello - I have the below function (source: http://ask.sqlteam.com/questions/110...urther-queries) that basically calculates the business hours/minutes elapsed between two **smalldatetime** fields:

Expand|Select|Wrap|Line Numbers
  1. set ANSI_NULLS ON
  2. set QUOTED_IDENTIFIER ON
  3. go
  4.  
  5. ALTER FUNCTION [dbo].[getBusinessHours] (@smalldatetime1 smalldatetime, @smalldatetime2 smalldatetime)
  6.  
  7. RETURNS bigint 
  8.  
  9. AS
  10.  
  11. BEGIN
  12.  
  13. DECLARE @Diff bigint; 
  14. DECLARE @adjusted_1 smalldatetime; 
  15. DECLARE @adjusted_2 smalldatetime;
  16.  
  17. SET @adjusted_1 = case when @smalldatetime1 - dateadd(day, datediff(day, 0, @smalldatetime1), 0) < '18:00' then @smalldatetime1 else dateadd(day, datediff(day, 0, @smalldatetime1), 0) + case when datename(weekday,@smalldatetime1) in ('saturday','sunday') then '17:30' else '18:00' end end;
  18.  
  19. SET @adjusted_2 = case when @smalldatetime2 - dateadd(day, datediff(day, 0, @smalldatetime2), 0) > '08:30' then @smalldatetime2 else dateadd(day, datediff(day, 0, @smalldatetime2), 0) + case when datename(weekday,@smalldatetime1) in ('saturday','sunday') then '09:30' else '08:30' end end;
  20.  
  21. SET @Diff = case when datename(weekday,@smalldatetime1) in ('saturday','sunday') then datediff(minute, @adjusted_1, @adjusted_2) - (datediff(day, @adjusted_1, @adjusted_2) * 960) else datediff(minute, @adjusted_1, @adjusted_2) - (datediff(day, @adjusted_1, @adjusted_2) * 870) end;
  22.  
  23. return @Diff
  24.  
  25. END
The problem I am facing is that the function fails to calculate the business hours correctly if the "**smalldatetime1**" falls between **12:00 AM** and **8:30 AM**.

Also FYI -

- smalldatetime1 is the date/time when a call is logged into the database.
- smalldatetime2 is the date/time when the call was closed.

Now basically I would like to track the calls that took more than 24 hrs / 4 hrs to close, only considering business hours, which is:

- 8:30 AM - 6:00 PM on Weekdays
- 9:30 AM - 5:30 PM on Weekends

Any help would be much appreciated.

Thanks!
Jul 1 '10 #1

✓ answered by Delerna

How about this

In your function the first thing you should do is check @smalldatetime1 to see if it is less than the start time for that day.
If it is then adjust @smalldatetime1 so that it is equal to the start time for that day.

Now do your calciulation using the adjusted
@smalldatetime1

2 4461
Delerna
1,134 Expert 1GB
How about this

In your function the first thing you should do is check @smalldatetime1 to see if it is less than the start time for that day.
If it is then adjust @smalldatetime1 so that it is equal to the start time for that day.

Now do your calciulation using the adjusted
@smalldatetime1
Jul 13 '10 #2
@Delerna
Hello - thanks for your response.

Much appreciate it.
Jul 13 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Monty | last post by:
Let's say you provide an online service from 7:00AM to 6:00PM Eastern Time (daylight time in the summer). Is there way of showing these hours of availability on a web page in the user's local...
1
by: Mel | last post by:
Hi, I have a text I called txtdate which is the system date or date today.How can I set the txtdate.text=8/29/04 even if the date today is equal to 8/30/04.Our business hours starts from 8am...
5
by: mitchchristensen | last post by:
I have a transaction log that tracks issues from a call center. Each time an issue is assigned to someone else, closed, etc. I get a time stamp. I have these time stamps for the beginning of an...
18
by: pb648174 | last post by:
Greeting, below is the complete SQL taken from aspfaq.com (retrieved from this newsgroup I believe) The query takes about two minutes to run. Does anybody have a better set based way (sub-second...
7
by: Sam | last post by:
Hi, I use C# in my ASP.NET projects. Here's what I need to do: I want to add x business days to a given date i.e. add 12 business days to today's date. What is the best, fastest and most...
3
by: iceone | last post by:
Hi everybody, I am monitoring a call center and i need to calculate the working hours between the moment the call is answered and the moment the call is closed. i need to calculate the working...
10
bsmeena7005
by: bsmeena7005 | last post by:
Hi help me to calculate the working hours between 10 to 18 hrs between two dates including the weekends also no braek or lunch time Example :- start date #01-08-2009 08:45:23# end date ...
10
by: LSGKelly | last post by:
I have some code in my database that calculates time between two date/time fields and also calculates the weekends and holidays. I did not create this code, so I'm at a bit of a loss why it's not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.