all groups > sql server (alternate) > august 2003 >
You're in the

sql server (alternate)

group:

Date/Time overlaps - urgent.


Date/Time overlaps - urgent. axion_sa
8/28/2003 3:28:35 AM
sql server (alternate):

Hi,



What I have is a booking table, and when updating/inserting I need to
ensure that there are no date/time overlaps. The problem I'm having is
that while the following script works for events on the same day, it
fails miserably when a booking starts on a previous day.



I've just spent the last hour going through previous posts and just
can't seem to it right.



My DB structure (Sql Server 2000):

Table: CollateralBooking

-- CBID - int, identity(1, 1)

-- CBcPartNumber - varchar(50) (foreign key)

-- CBdDateTimeFrom - smalldatetime

-- CBdDateTimeTo - smalldatetime

-- CBcAlias - varchar(50) (foreign key)



My current script (in a stored proc):


IF (SELECT COUNT(*) FROM CollateralBooking
WHERE (((@CBdDateTimeFrom > CBdDateTimeFrom) AND (@CBdDateTimeFrom < CBdDateTimeTo))
OR ((@CBdDateTimeTo > CBdDateTimeFrom) AND (@CBdDateTimeTo < CBdDateTimeTo)))
AND (CBcPartNumber = @CBcPartNumber)) <> 0
BEGIN
-- Return an error.
END

-- ... Other checks & finally, the insert/update.


--
Re: Date/Time overlaps - urgent. axion_sa
8/28/2003 8:00:11 AM

Thanks David :) It's working well now.



On a side note, CBID was the primary key.


--
Re: Date/Time overlaps - urgent. David Portas
8/28/2003 11:59:25 AM
First, add a constraint, if you haven't already, to ensure that the "from"
datetime is less than the "to" datetime (I've guessed your primary key and
only included the essential columns)

CREATE TABLE CollateralBooking (cbid INTEGER UNIQUE, cbcpartnumber INTEGER,
cbddatetimefrom DATETIME NOT NULL, cbddatetimeto DATETIME NOT NULL, CHECK
(cbddatetimefrom<cbddatetimeto), PRIMARY KEY (cbcpartnumber,
cbddatetimefrom))

IF EXISTS
(SELECT *
FROM CollateralBooking AS A
JOIN CollateralBooking AS B
ON A.cbcpartnumber=B.cbcpartnumber AND A.cbid<>B.cbid
AND NOT (A.cbddatetimefrom > B.cbddatetimeto
OR A.cbddatetimeto < B.cbddatetimefrom))
/* Raise an error */

--
David Portas
------------
Please reply only to the newsgroup
--

AddThis Social Bookmark Button