473,511 Members | 12,747 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calculating Time Difference

Hi,

I have a table called Bookings which has two important columns;
Booking_Start_Time and Booking_End_Time. These columns are both of type
DATETIME. Given any day how can I calculate how many hours are available
between the hours of 09.00 and 17.30 so a user can see at a glance how many
hours they have unbooked on a particular day (i.e. 8.5 hours less the time
of any bookings on that day), can this be done with a query
or do I have to work it out in my code?

Thanks for your help
Jul 20 '05 #1
2 47844
Shaun,

Take a look at DateDiff function in books online. Something like this will
help you. This query return the available time in minutes.

select Date,
DateDiff(minute, '09:00','17:30') - Sum(BookTime)
from (select convert(char(10), Booking_Start_Time, 112) as Date,
DateDiff(minute, Booking_Start_Time, Booking_End_Time) as
BookTime
from YourTable
) as Tmp
group by Date

Shervin

"Shaun" <sh***@mania.plus.com> wrote in message
news:bl**********@reader-00.news.insnet.cw.net...
Hi,

I have a table called Bookings which has two important columns;
Booking_Start_Time and Booking_End_Time. These columns are both of type
DATETIME. Given any day how can I calculate how many hours are available
between the hours of 09.00 and 17.30 so a user can see at a glance how many hours they have unbooked on a particular day (i.e. 8.5 hours less the time
of any bookings on that day), can this be done with a query
or do I have to work it out in my code?

Thanks for your help

Jul 20 '05 #2
"Shaun" <sh***@mania.plus.com> wrote in message news:bl**********@reader-00.news.insnet.cw.net...
Hi,

I have a table called Bookings which has two important columns;
Booking_Start_Time and Booking_End_Time. These columns are both of type
DATETIME. Given any day how can I calculate how many hours are available
between the hours of 09.00 and 17.30 so a user can see at a glance how many
hours they have unbooked on a particular day (i.e. 8.5 hours less the time
of any bookings on that day), can this be done with a query
or do I have to work it out in my code?

Thanks for your help


Here's a solution that allows bookings to overlap, multiple interested
non-overlapping intervals to be specified (you had specified one, 0900
to 1730) and both bookings and interested intervals to span more than
one day.

-- Bookings can overlap
CREATE TABLE Bookings
(
booking_start_time DATETIME NOT NULL,
booking_end_time DATETIME NOT NULL,
CHECK (booking_end_time >= booking_start_time),
PRIMARY KEY (booking_start_time, booking_end_time)
)

-- Sample data
INSERT INTO Bookings (booking_start_time, booking_end_time)
VALUES ('20030930 8:30AM', '20030930 10:30AM')
INSERT INTO Bookings (booking_start_time, booking_end_time)
VALUES ('20030930 10:00 AM', '20030930 12:30PM')
INSERT INTO Bookings (booking_start_time, booking_end_time)
VALUES ('20030930 2:00PM', '20030930 4:30PM')
INSERT INTO Bookings (booking_start_time, booking_end_time)
VALUES ('20030930 9:00PM', '20030930 11:30PM')

-- Interested intervals can't overlap
CREATE TABLE InterestedIntervals
(
interval_start DATETIME NOT NULL,
interval_end DATETIME NOT NULL,
CHECK (interval_end >= interval_start),
PRIMARY KEY (interval_start, interval_end)
)

-- Sample data
-- Note that multiple interested intervals can be specified
INSERT INTO InterestedIntervals (interval_start, interval_end)
VALUES ('20030930 8:00AM', '20030930 1:00PM')
INSERT INTO InterestedIntervals (interval_start, interval_end)
VALUES ('20030930 2:00PM', '20030930 6:00PM')
INSERT INTO InterestedIntervals (interval_start, interval_end)
VALUES ('20030930 8:00PM', '20030930 11:59PM')

CREATE VIEW OrderedBookings (interval_start, interval_end, seq)
AS
SELECT B1.booking_start_time,
B1.booking_end_time,
(SELECT COUNT(*)
FROM Bookings AS B2
WHERE B2.booking_start_time < B1.booking_start_time OR
(B2.booking_start_time = B1.booking_start_time AND
B2.booking_end_time < B1.booking_end_time))
FROM Bookings AS B1

-- A DATETIME value can be from date 17530101 to date 99991231
CREATE VIEW BookingGaps (interval_start, interval_end)
AS
SELECT '17530101' AS interval_start, interval_start AS interval_end
FROM OrderedBookings
WHERE seq = 0 AND interval_start > '17530101'
UNION ALL
SELECT OI1.interval_end AS interval_start,
COALESCE(OI2.interval_start, '99991231') AS interval_end
FROM OrderedBookings AS OI1
LEFT OUTER JOIN
OrderedBookings AS OI2
ON OI2.seq = OI1.seq + 1
WHERE OI2.interval_start > OI1.interval_end OR
OI2.seq IS NULL

CREATE VIEW InterestedBookingGaps (interval_start, interval_end)
AS
SELECT CASE WHEN G.interval_start BETWEEN
I.interval_start AND I.interval_end
THEN G.interval_start
ELSE I.interval_start
END,
CASE WHEN G.interval_end BETWEEN
I.interval_start AND I.interval_end
THEN G.interval_end
ELSE I.interval_end
END
FROM BookingGaps AS G
INNER JOIN
InterestedIntervals AS I
ON NOT(I.interval_start >= G.interval_end OR
I.interval_end <= G.interval_start)

-- Booking gaps with respect to interested intervals
SELECT interval_start, interval_end
FROM InterestedBookingGaps
ORDER BY interval_start

interval_start interval_end
2003-09-30 08:00:00.000 2003-09-30 08:30:00.000
2003-09-30 12:30:00.000 2003-09-30 13:00:00.000
2003-09-30 16:30:00.000 2003-09-30 18:00:00.000
2003-09-30 20:00:00.000 2003-09-30 21:00:00.000
2003-09-30 23:30:00.000 2003-09-30 23:59:00.000

-- Free time
SELECT COALESCE(SUM(DATEDIFF(MINUTE, interval_start, interval_end))
/ 60.0, 0) AS hours_free,
COALESCE(SUM(DATEDIFF(MINUTE, interval_start, interval_end)), 0)
AS minutes_free
FROM InterestedBookingGaps

hours_free minutes_free
3.983333 239

Regards,
jag
Jul 20 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
4437
by: Hans Gruber | last post by:
Hi all, I have been struggling with a problem all day, I have been unable to come up with a working solution. I want to write a function which takes 2 unix timestamps and calculates the...
1
8987
by: Shaun Thornburgh | last post by:
Hi, I have a table called Bookings which has two important columns; Booking_Start_Time and Booking_End_Time. These columns are both of type DATETIME. Given any day how can I calculate how many...
4
2172
by: luscus | last post by:
I am trying to device a formula so that when i check of a yes/no box (done) it will automatically add the time in a field called "End Time" and at the same time stamp the amount of minutes...
3
2680
by: luscus | last post by:
Thanks for all the responses on my first question. Unfortunately the answers I was given were too complicated for my small brain , and neophite condition to understand. So if you could talk down to...
3
14877
by: Richard | last post by:
Hi, Is there any way to get the time difference between Central(US) and GMT in vb.net. I'll appreciate your help/suggestion. Thanks RC
4
31193
by: iamonthisboat | last post by:
I have a data set like so: UTC_TIME Timestamp NodeID Message Flag Line Station 11/19/2005 10:45:07 1132397107.91 1 3 5 1028 1034...
3
3425
by: Steve | last post by:
I am trying to calculate elapsed travel times for flights. My plan is to enter the local departure time, the departure city and the local arrival time and city. These times would be standardised...
2
1959
by: RN1 | last post by:
This is how I am calculating the time difference: ------------------------ Function TimeDiff(ByVal StartDateTime, ByVal EndDateTime) Dim h As Integer Dim m As Integer Dim t1 As DateTime Dim...
3
1730
by: mfaisalwarraich | last post by:
Hi everybody. I have some problem while calculating time. i have employees who are working on hourly basis. i want to calculate the time. for example if an employee start working at 11:00pm...
0
7148
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
7367
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
7430
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7089
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
7517
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
4743
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...
0
3217
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1581
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
790
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.