简体   繁体   English

SQL DateTime比较

[英]SQL DateTime comparing

I have a calender where a user can create appointments, I don't want double bookings nor do i want bookings overlapping one another. 我有一个日历,用户可以在其中创建约会,我既不想重复预订,也不想重复预订。

I think to achieve this the following logic would do the trick. 我认为,要实现这一目标,可以使用以下逻辑。

Do not Allow bookings when: 在以下情况下不允许预订:

@StartDateRequested <= Appointments.StartDate AND EndDateRequested >= Appointments.EndDate @StartDateRequested >= Appointments.StartDate AND EndDateRequested <= Appointments.EndDate @StartDateRequested <= Appointments.StartDate和EndDateRequested> = Appointments.EndDate @StartDateRequested> = Appointments.StartDate AND EndDateRequested <= Appointments.EndDate

This Should stop bookings that are within a another bookings start and end, and stop bookings that start before and end after. 这应停止另一个预订开始和结束之内的预订,并停止之前和之后结束的预订。

This is what i have tried: 这是我尝试过的:

DECLARE @ScheduledStart datetime2
SET @ScheduledStart = '2014/07/21 11:00:00 PM'

DECLARE @ScheduledEnd datetime2
SET @ScheduledEnd = '2014/07/21 13:55:00 PM'

DECLARE
    @ErrorMessage varchar(128)
    ,@IsLocalTrans bit

DECLARE @Count int

SELECT
    --@Count = COUNT(*)
    A.AppointmentId
FROM
    Appointment AS A WITH (NOLOCK)
WHERE
    (
    --  (@ScheduledStart BETWEEN A.ScheduledStart AND A.ScheduledEnd)
    --OR
    --  (@ScheduledEnd BETWEEN A.ScheduledStart AND A.ScheduledEnd)
    --OR
        (@ScheduledStart < A.ScheduledStart AND @ScheduledEnd > A.ScheduledEnd)
    OR
        (@ScheduledStart > A.ScheduledStart AND @ScheduledEnd < A.ScheduledEnd)
    )
    AND A.AppointmentStatusId = 2 --confirmed

This has failed it returned 3 appointments which start and end time were both greater then the start and end times of the dates i tried. 失败了,它返回了3个约会,这些约会的开始和结束时间都比我尝试的日期的开始和结束时间长。

The commented out code, worked for stopping a booking if it started or ended in another bookings slot, but did not cater for bookings that overlap. 注释掉的代码可用于在另一个预订时段开始或结束预订时停止该预订,但不能满足重叠的预订。

I think i'm on the right track, just my sql datetime logic is off. 我认为我处在正确的轨道上,只是我的sql datetime逻辑已关闭。

In my database i have an appointment that starts at 12:00 to 12:55, You will see in my testing values i'm trying to check if i can create an appointment for 11:00 to 13:55, This should not allow me to do this. 在我的数据库中,我的约会开始于12:00到12:55,您将在我的测试值中看到我正在尝试检查是否可以为11:00到13:55创建约会,这不应该允许我要这样做。

This logic for overlaps of time is that there is an overlap when one starts before the other ends and the other ends after the first starts. 时间重叠的逻辑是,当一个在另一个结束之前开始,而另一个在第一次开始之后结束时,存在重叠。 The following logic returns overlaps: 以下逻辑返回重叠:

WHERE (@ScheduledStart <= A.ScheduledEnd AND
       @ScheduledEnd >= A.ScheduledStart
      )

The following returns non-overlaps: 以下返回非重叠:

WHERE (@ScheduledStart > A.ScheduledEnd OR
       @ScheduledEnd < A.ScheduledStart
      )

The Logic i used in the end looked like this: 我最终使用的逻辑如下所示:

WHERE
(
    (@ScheduledStart BETWEEN A.ScheduledStart AND A.ScheduledEnd)
OR
    (@ScheduledEnd BETWEEN A.ScheduledStart AND A.ScheduledEnd)
OR
    (A.ScheduledStart BETWEEN @ScheduledStart AND @ScheduledEnd)
OR
    (A.ScheduledEnd BETWEEN @ScheduledStart AND @ScheduledEnd)
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM