Checking if a given date fits between a range of dates

前端 未结 3 1631
情歌与酒
情歌与酒 2021-02-07 03:39

If I have 2 date columns in a table, startDate and endDate. How do I return rows where a given date fits between those 2 dates? For example:

3条回答
  •  眼角桃花
    2021-02-07 04:37

    for other lappingcheck the following might be interesting

    Select * from sted where [dbo].[F_LappingDays](Startdate,EndDate,'20121025','20121025')=1
    
    
    CREATE Function [dbo].[F_LappingDays](@Von1 datetime,@bis1 Datetime,@von2 Datetime,@bis2 Datetime) Returns int as
    /*
    20110531 Thomas Wassermann
    Terminüberschneidungen finden
    */
    begin
    Declare @Result int
    
    Select @Result  = 0
    if (@Von1>=@Von2) and (@bis1<=@Bis2)
        begin
            Select @Result=Cast(@Bis1 - @von1 + 1 as Int)
        end
    
    else if (@Von1<=@Von2) and (@bis1 > @Von2) and (@bis1<=@Bis2)
        begin
            Select @Result=Cast(@Bis1 - @von2 + 1 as Int)
        end
    
    else if (@Von1>=@Von2) and (@von1<=@bis2) and (@bis1>@Bis2)
        begin
            Select @Result=Cast(@Bis2 - @von1 + 1 as Int)
        end
    
    else if (@Von1<@Von2) and (@bis1>@Bis2)
        begin
            Select @Result=Cast(@Bis2 - @von2 + 1 as Int)
        end
    
    
    
    Return @Result
    end 
    

提交回复
热议问题