SQL query to select dates between two dates

后端 未结 22 1377
囚心锁ツ
囚心锁ツ 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:52

    if its date in 24 hours and start in morning and end in the night should add something like :

    declare @Approval_date datetime
    set @Approval_date =getdate()
    Approval_date between @Approval_date +' 00:00:00.000' and @Approval_date +' 23:59:59.999'
    
    0 讨论(0)
  • 2020-11-22 09:53
    select * from test 
         where CAST(AddTime as datetime) between '2013/4/4' and '2014/4/4'
    

    -- if data type is different

    0 讨论(0)
  • 2020-11-22 09:53

    I would go for

    select Date,TotalAllowance from Calculation where EmployeeId=1
                 and Date >= '2011/02/25' and Date < DATEADD(d, 1, '2011/02/27')
    

    The logic being that >= includes the whole start date and < excludes the end date, so we add one unit to the end date. This can adapted for months, for instance:

    select Date, ... from ...
                 where Date >= $start_month_day_1 and Date < DATEADD(m, 1, $end_month_day_1)
    
    0 讨论(0)
  • 2020-11-22 09:54
    SELECT Date, TotalAllowance  
    FROM Calculation  
    WHERE EmployeeId = 1 
      AND Date BETWEEN to_date('2011/02/25','yyyy-mm-dd') 
                   AND to_date ('2011/02/27','yyyy-mm-dd');
    
    0 讨论(0)
提交回复
热议问题