views:

409

answers:

2

I am having start time and end time as two fields in my database of type Datetime, Now i want that start time and end time selected by user should not fall within the interval of start time and end time that are already present in database. How to do this validation.

+3  A: 

Firstly, ensure that yourStartTime < yourEndTime, if you haven't already.

Then you can run this query on your database:

SELECT COUNT (*) FROM [table]
WHERE [table].EndTime > yourStartTime
      AND [table].StartTime < yourEndTime

If the count is non-zero, you've failed validation.

Dan Puzey
Add some brackets around the * :)
Neurofluxation
Bah, I knew I couldn't remember my SQL properly :-D Thx!
Dan Puzey
Now why was this voted down? :-|
Dan Puzey
+2  A: 

You could use TimeSpan ts = endTime.Subtract(startTime); which will give you the difference between the 2 dates. Then, selecting the results from the database you can use the DATEDIFF method in order to compare these values.

If I have understood the question correctly.

Ardman
What use is the timespan?
Dan Puzey
You get some nice methods which you can use in order to workout how many Days, Hours, Minutes etc. have passed.
Ardman
Yes, but I don't think there's a relevance between the duration between two dates, and whether or not the range between them overlaps.
Dan Puzey