SQL query to select dates between two dates

后端 未结 22 1375
囚心锁ツ
囚心锁ツ 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:29
    /****** Script for SelectTopNRows command from SSMS  ******/
    SELECT TOP 10 [Id]
      ,[Id_parvandeh]
      ,[FirstName]
      ,[LastName]
      ,[RegDate]
      ,[Gilder]
      ,[Nationality]
      ,[Educ]
      ,[PhoneNumber]
      ,[DueInMashhad]
    
      ,[EzdevajDate]
    
    
      ,[MarriageStatus]
      ,[Gender]
      ,[Photo]
    
      ,[ModifiedOn]
      ,[CreatorIp]
       From
      [dbo].[Socials] where educ >= 3 or EzdevajDate  >= '1992/03/31' and EzdevajDate <= '2019/03/09' and MarriageStatus = 1
    
    0 讨论(0)
  • 2020-11-22 09:30

    you should put those two dates between single quotes like..

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

    or can use

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

    keep in mind that the first date is inclusive, but the second is exclusive, as it effectively is '2011/02/27 00:00:00'

    0 讨论(0)
  • 2020-11-22 09:32
    select * from table_name where col_Date between '2011/02/25' 
    AND DATEADD(s,-1,DATEADD(d,1,'2011/02/27'))
    

    Here, first add a day to the current endDate, it will be 2011-02-28 00:00:00, then you subtract one second to make the end date 2011-02-27 23:59:59. By doing this, you can get all the dates between the given intervals.

    output:
    2011/02/25
    2011/02/26
    2011/02/27
    
    0 讨论(0)
  • best query for the select date between current date and back three days:

      select Date,TotalAllowance from Calculation where EmployeeId=1 and Date BETWEEN       
    DATE_SUB(CURDATE(), INTERVAL 3 DAY)  AND CURDATE() 
    

    best query for the select date between current date and next three days:

      select Date,TotalAllowance from Calculation where EmployeeId=1 and Date BETWEEN   
       CURDATE()  AND DATE_ADD(CURDATE(), INTERVAL 3 DAY)   
    
    0 讨论(0)
  • 2020-11-22 09:34
    select Date,TotalAllowance 
    from Calculation 
    where EmployeeId=1
      and convert(varchar(10),Date,111) between '2011/02/25' and '2011/02/27'
    
    0 讨论(0)
  • 2020-11-22 09:34

    Try putting the dates between # # for example:

    #2013/4/4# and #2013/4/20#
    

    It worked for me.

    0 讨论(0)
提交回复
热议问题