SQL “between” not inclusive

后端 未结 8 1681
情歌与酒
情歌与酒 2020-11-30 17:05

I have a query like this:

SELECT * FROM Cases WHERE created_at BETWEEN \'2013-05-01\' AND \'2013-05-01\'

But this gives no results even tho

8条回答
  •  无人及你
    2020-11-30 17:51

    I find that the best solution to comparing a datetime field to a date field is the following:

    DECLARE @StartDate DATE = '5/1/2013', 
            @EndDate   DATE = '5/1/2013' 
    
    SELECT * 
    FROM   cases 
    WHERE  Datediff(day, created_at, @StartDate) <= 0 
           AND Datediff(day, created_at, @EndDate) >= 0 
    

    This is equivalent to an inclusive between statement as it includes both the start and end date as well as those that fall between.

提交回复
热议问题