SQL query to select dates between two dates

后端 未结 22 1839
囚心锁ツ
囚心锁ツ 2020-11-22 09:10

I have a start_date and end_date. I want to get the list of dates in between these two dates. Can anyone help me pointing the mistake in my query.<

22条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 09:45

    Since a datetime without a specified time segment will have a value of date 00:00:00.000, if you want to be sure you get all the dates in your range, you must either supply the time for your ending date or increase your ending date and use <.

    select Date,TotalAllowance from Calculation where EmployeeId=1 
    and Date between '2011/02/25' and '2011/02/27 23:59:59.999'
    

    OR

    select Date,TotalAllowance from Calculation where EmployeeId=1 
    and Date >= '2011/02/25' and Date < '2011/02/28'
    

    OR

    select Date,TotalAllowance from Calculation where EmployeeId=1 
    and Date >= '2011/02/25' and Date <= '2011/02/27 23:59:59.999'
    

    DO NOT use the following, as it could return some records from 2011/02/28 if their times are 00:00:00.000.

    select Date,TotalAllowance from Calculation where EmployeeId=1 
    and Date between '2011/02/25' and '2011/02/28'
    

提交回复
热议问题