Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 5th, 2007, 11:21 AM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Simple but tricky problem...

I've a serious problem...

1.Consider a Conference Room Booking scenario
where in room reservation is stored as two columns : DateTimeFrom & DateTimeTo.

2. Now, i want to compare a new user entered values; To & From dates with existing records,

if entered values lies between the time range of DateTimeFrom & DateTimeTo
then reject else Accept entry.

i.e.
If room is already reserved for time 2.00(DateTimeFrom ) to 6.00 (DateTimeTo);
then it shouldnt accept entry for time range say, 3.00 to 4.00

Table :
intRoomId| dtmStart | dtmEnd
__________________________________________________ ______
1 |2007-12-28 02:32:00 |2007-12-28 04:32:20
1 |2007-12-28 08:32:00 |2007-12-28 10:32:20

Now,
1. new Entry for time between 2.00 to 4.00 & 8.00 to 10.00 should be rejected.
2. new entry for time between entire slot say 1.00 to 11.00 should also be rejected.

i've tried this :
Code:

Select intID, 
SUM(CASE WHEN  CAST(@UserDateFrom AS DATETIME) >= dtmStart AND  CAST(@UserDateTo AS DATETIME) < dtmEnd
        THEN 1 ELSE 0 END)
from TestDates
group by intID

I've tried a lot but its not working out.. SQL pros..plz help
thanks in advance
 
Old December 5th, 2007, 08:07 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

Here's a suggestion that works in a query in my app:

SELECT dbo.getTime(InspStart) AS InspStart, dbo.getTime(InspEnd) AS InspEnd FROM Inspections WHERE RoomID = 22 AND
((InspStart < '12/5/2007 6:30:01 AM' AND '12/5/2007 6:30:01 AM' < InspEnd) OR (InspStart < '12/5/2007 2:00:01 PM' AND '12/5/2007 2:00:01 PM' < InspEnd) OR (InspStart = '12/5/2007 6:30:01 AM' AND InspEnd = '12/5/2007 2:00:01 PM')) ORDER BY WorkID;

You can study this query and see that it checks to see if the requested start and end times are within any start and end times, checks to see if the requested times overlap either end of an existing reservation. It also checks to see if either the requested start or end times equal any existing reservation times

 
Old December 6th, 2007, 05:08 AM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for your suggestion.
But your query fails for one condition :

if room is booked from 2.00 to 4.00 and 8.00 to 10.00 ;
and
user sends time 1.00 to 11.00 ,that is entire time slot,
overriding previous reservation.

This is also a invalid entry & hence shouldn't be accepted.
query fails for this condition

waitin for your reply...

 
Old December 6th, 2007, 01:10 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

So what's the answer? I've given you a great head start. Even if you don't know the syntax, what's the logic? Thank you for pointing that out. My time system won't accept that time slot now.

 
Old December 6th, 2007, 04:45 PM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Either

SELECT *
FROM Table1
WHERE @WantedStart < dtmEnd AND @WantedEnd > dtmStart

SELECT *
FROM Table1
WHERE @WantedStart > dtmEnd OR @WantedEnd < dtmStart


 
Old December 7th, 2007, 05:47 AM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

@rstelma : Can you post your new query then

I've a cursor which handles all the possibilities..
If u have a better answer then please post it..

here's the working cursor:
Code:
CREATE PROC DateTimeSlotCURSOR 
@dtmUserFrom     DATETIME,
@dtmUserTo         DATETIME,
@intRoomID         INT
AS
BEGIN

    DECLARE @PFrom DATETIME
    DECLARE @PTo DATETIME

    DECLARE test CURSOR 
        FOR select dtmFrom,dtmTo FROM TestDates WHERE intRoomId = @intRoomID
    OPEN test
    FETCH NEXT FROM test INTO @PFrom,@PTo
    WHILE (@@FETCH_STATUS=0)
    BEGIN
        IF(@dtmUserFrom = @PFrom OR @dtmUserTo=@PTo)
        BEGIN
            PRINT 'Reserved'
            CLOSE test
            DEALLOCATE test
            RETURN 0
        END
        ELSE
        BEGIN
            IF(@PFrom > @dtmUserFrom AND @PFrom < @dtmUserTo OR @PTo > @dtmUserFrom AND @PTo < @dtmUserTo)
            BEGIN
                PRINT 'Reserved'
                CLOSE test
                DEALLOCATE test
                RETURN 0
            END
            ELSE
            BEGIN
                IF(@dtmUserFrom > @PFrom AND @dtmUserTo < @PTo)
                BEGIN
                    PRINT 'Reserved'
                    CLOSE test
                    DEALLOCATE test
                    RETURN 0
                END
            END
        END
    FETCH NEXT FROM test INTO @PFrom,@PTo
    END
    PRINT 'AVAILABLE'
    CLOSE test
    DEALLOCATE test
    RETURN 1
END

--EXEC DateTimeSlotCURSOR '2007-11-20 01:00:00','2007-11-20 10:00:00',1
 
Old December 7th, 2007, 08:20 PM
Friend of Wrox
 
Join Date: Jul 2003
Posts: 599
Thanks: 6
Thanked 3 Times in 3 Posts
Default

You don't really need a cursor for this.

All I needed to change in my query above was:

OR (InspStart = '12/5/2007 6:30:01 AM' AND InspEnd = '12/5/2007 2:00:01 PM')

to:

OR (InspStart >= '12/5/2007 6:30:01 AM' AND InspEnd <= '12/5/2007 2:00:01 PM')

Hope this helps.

Richard


 
Old December 8th, 2007, 12:07 PM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Don't let Peso's deceptively simple solution pass you by because it IS the correct solution.

--Jeff Moden
 
Old December 10th, 2007, 04:41 AM
Registered User
 
Join Date: Dec 2007
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

@Jeff Moden
Yes Peso's solution is a correct solution , but its not a complete solution. It doesnt handle all the conditions that i have mentioned.

@rstelma
Your corrected query still doesnt give desired result for entire time slot.. Please go through it again...

I know cursor is not the best answer but currently its the only working solution that i have... any suggestions are most welcome...
thnx :)



 
Old December 10th, 2007, 10:05 AM
Friend of Wrox
 
Join Date: Oct 2006
Posts: 475
Thanks: 0
Thanked 9 Times in 9 Posts
Default

Quote:
quote:Originally posted by avdhut_s
 @Jeff Moden
Yes Peso's solution is a correct solution , but its not a complete solution. It doesnt handle all the conditions that i have mentioned.

@rstelma
Your corrected query still doesnt give desired result for entire time slot.. Please go through it again...

I know cursor is not the best answer but currently its the only working solution that i have... any suggestions are most welcome...
thnx :)
Ok then... I suggest you study Peso's answer :D... and like any good programmer, adapt it to become the "complete" answer you want... his only intent was to show you the principle of the best method... you should be able to easily adapt it.

--Jeff Moden





Similar Threads
Thread Thread Starter Forum Replies Last Post
A Simple Problem paole Wrox Book Feedback 7 July 23rd, 2007 08:55 PM
Aligning two chart plot areas - simple but tricky! Alseikhan Excel VBA 0 March 20th, 2006 07:18 PM
Simple problem rsa3des Classic ASP Basics 4 August 8th, 2005 06:14 PM
Tricky asp/sql problem cedwards Dreamweaver (all versions) 8 June 13th, 2005 04:02 PM
Tricky Javascript/XSL '&' problem...... robster Javascript How-To 1 January 23rd, 2004 02:11 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.