且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

获取两个日期之间的记录

更新时间:2023-01-30 10:41:13

0。这些日期似乎是varchar类型,如果是这样,你应该将它们更改为日期类型。

1.看看你的例子,似乎每个假期只持续一天,如果是这样,为什么需要两个约会 - fromdate和todate?在这种情况下,您只需要一个日期字段,例如date_of_holiday,然后获取其记录的数量(date_of_holiday介于fromdate和todate之间)并且您已完成。

2.如果假期超过一天,请考虑以下情况:

2.1(leave_start_date和leave_end_date之间的holiday_from_date)AND(holiday_to_date> leave_end_date)或

2.2(leave_start_date和leave_end_date之间的holiday_to_date)AND(holiday_from_date< leave_start_date)或

2.3(leave_start_date和leave_end_date之间的holiday_from_date)AND(leave_start_date和leave_end_date之间的holiday_to_date)
0. Those dates seem to be of varchar type, if so, you should change them to date type.
1. Looking at your example, it seems that each holiday only lasts one day, if so, why need two dates - fromdate and todate? In such a case, you only need a date field say date_of_holiday, then get a count of records whose (date_of_holiday between fromdate and todate) and you are done.
2. If there are holidays that span more that one day, then consider these scenarios:
2.1 (holiday_from_date between leave_start_date and leave_end_date) AND (holiday_to_date > leave_end_date) OR
2.2 (holiday_to_date between leave_start_date and leave_end_date) AND (holiday_from_date < leave_start_date) OR
2.3 (holiday_from_date between leave_start_date and leave_end_date) AND (holiday_to_date between leave_start_date and leave_end_date)


您是否正在寻找重叠拟议假期的假期计数?

Are you looking for a count of holidays which overlap the proposed leave?
SELECT
    COUNT(*) As OverlappingCount
FROM
    ListOfHolidays
WHERE
    FromDate <= @ToDate
And
    ToDate >= @FromDate
;