Skip to content
Advertisement

T-Sql 2005 Adding hours to a datetime field with the result within working hours

I have two Datetime fields that I wish to add together. They are in the following format: ’01/01/1900 00:00:00′.

The main issue with this is that I want the calculation to only include working hours. The working day is between 08:30 and 17:30 and does not include weekends:

Also if the first field starts out of the working day or is on a weekend then the second field should be added from the start of the next working day.

For example:

`’26/06/2009 15:45:00′ + ’01/01/1900 09:00:00′ = ’29/06/1900 15:45:00′

’12/07/2009 14:22:36′ + ’01/01/1900 18:00:00′ = ’13/07/1900 08:30:00′

’15/07/2009 08:50:00′ + ’01/01/1900 04:00:00′ = ’15/07/2009 12:50:00’`

Im pretty sure that this is going to involve creating a user defined function to work this out but I have no idea how to even start this(I am quite out of my depth here) Could anyone offer me some advice on how to achieve this?

Advertisement

Answer

try this, you might have to put it in a function

Hope that helps

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement