473,799 Members | 3,161 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Date/Time overlaps - urgent.


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: CollateralBooki ng

-- 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 CollateralBooki ng
WHERE (((@CBdDateTime From > CBdDateTimeFrom ) AND (@CBdDateTimeFr om < CBdDateTimeTo))
OR ((@CBdDateTimeT o > CBdDateTimeFrom ) AND (@CBdDateTimeTo < CBdDateTimeTo)) )
AND (CBcPartNumber = @CBcPartNumber) ) <> 0
BEGIN
-- Return an error.
END

-- ... Other checks & finally, the insert/update.
--
Posted via http://dbforums.com
Jul 20 '05 #1
2 3955
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 CollateralBooki ng (cbid INTEGER UNIQUE, cbcpartnumber INTEGER,
cbddatetimefrom DATETIME NOT NULL, cbddatetimeto DATETIME NOT NULL, CHECK
(cbddatetimefro m<cbddatetimeto ), PRIMARY KEY (cbcpartnumber,
cbddatetimefrom ))

IF EXISTS
(SELECT *
FROM CollateralBooki ng AS A
JOIN CollateralBooki ng AS B
ON A.cbcpartnumber =B.cbcpartnumbe r AND A.cbid<>B.cbid
AND NOT (A.cbddatetimef rom > B.cbddatetimeto
OR A.cbddatetimeto < B.cbddatetimefr om))
/* Raise an error */

--
David Portas
------------
Please reply only to the newsgroup
--
Jul 20 '05 #2

Thanks David :) It's working well now.

On a side note, CBID was the primary key.
--
Posted via http://dbforums.com
Jul 20 '05 #3

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

Similar topics

4
3004
by: Lynn | last post by:
On a form I have Date_Start Date_End I have a new Date_Start1 Date_End1 which the use inputs. I need to validate that Date_Start1 and Date_End1 are a date range entirely separate from Date_Start and Date_End i.e. no overlapping
4
1820
by: Holger Marzen | last post by:
Say, we have uptimes from several servers: Server up_from up_to ------ ------- ------- s1 0:00 8:00 s1 10:00 20:00 s1 22:00 24:00 (would better be a timestamp with 0:00 and next day) s2 0:00 4:00 s2 6:00 23:00
9
4321
by: Stefan Bauer | last post by:
Hi NG, we've got a very urgent problem... :( We are importing data with the LOAD utility. The input DATE field data is in the format DDMMYYYY (for days) and MMYYYY (for months). The target data format is european DD.MM.YYYY (for days) and 01.MM.YYYY (for months). The input format is not recognizable as a DATE input to a DB2 LOAD for
2
4799
by: John Baker | last post by:
HI; I have a table where the user is entering data which shows scheduling for member of teams. One problem we have encountered is that sometimes the inputter (is this a word) puts in dates for an individual that overlap, creating all sorts of problems. One record is entered for each scheduled event (each one is a day in length or more), so that an individual will have multiple events on the table. For the purposes of this question,...
15
18896
by: Khurram | last post by:
I have a problem while inserting time value in the datetime Field. I want to Insert only time value in this format (08:15:39) into the SQL Date time Field. I tried to many ways, I can extract the value in timeonly format by using this command Format(now,"HH:mm:ss") But when I insert it into the Sql Server database, it embadded date value with it. the output looks like that "01/01/1900 08:59:00" in that case time is
6
3238
by: alexanderpope11 | last post by:
Hello, how do I write this SQL: I would like to check the Main table for invalid rows. An invalid row is: any row where the Start_date to stop_date range overlaps an invalid date in the Code table. For example, Row#2 is invalid because the Start_date-Stop_Date range overlaps 2 days in the code table where the code AA was not valid (12/30/2000 - 12/31/2000) Main Table
18
38248
by: dfetrow410 | last post by:
Anyone have some code that will do this? Dave
7
1442
by: James Conrad StJohn Foreman | last post by:
I've got bookings per day in a table: bkgdate bookings company 1/1/06 50 A 2/1/06 45 A 3/1/06 55 A 4/1/06 40 A 5/1/06 30 A 6/1/06 45 A 7/1/06 35 A
3
2337
by: Deano | last post by:
The short version; In short, given one date range (start and end dates) how can I find the period that overlaps with another date range? The long version; I have knocked up a little application that helps my friend monitor employee absences. You can enter the start and end dates of an absence. For reports the user specifies start and end dates which produces a list of people with absences
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10228
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9072
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.