SQL query to select dates between two dates

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

    Try this:

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

    The date values need to be typed as strings.

    To ensure future-proofing your query for SQL Server 2008 and higher, Date should be escaped because it's a reserved word in later versions.

    Bear in mind that the dates without times take midnight as their defaults, so you may not have the correct value there.

    0 讨论(0)
  • 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'
    
    0 讨论(0)
  • 2020-11-22 09:46

    we can use between to show two dates data but this will search the whole data and compare so it will make our process slow for huge data, so i suggest everyone to use datediff:

    qry = "SELECT * FROM [calender] WHERE datediff(day,'" & dt & "',[date])>=0 and datediff(day,'" & dt2 & "',[date])<=0 "
    

    here calender is the Table, dt as the starting date variable and dt2 is the finishing date variable.

    0 讨论(0)
  • 2020-11-22 09:47
    Select 
        * 
    from 
        Calculation 
    where 
        EmployeeId=1 and Date between #2011/02/25# and #2011/02/27#;
    
    0 讨论(0)
  • 2020-11-22 09:51

    This is very old, but given a lot of experiences I have had with dates, you might want to consider this: People use different regional settings, as such, some people (and some databases/computers, depending on regional settings) may read this date 11/12/2016 as 11th Dec 2016 or Nov 12, 2016. Even more, 16/11/12 supplied to MySQL database will be internally converted to 12 Nov 2016, while Access database running on a UK regional setting computer will interpret and store it as 16th Nov 2012.

    Therefore, I made it my policy to be explicit whenever I am going to interact with dates and databases. So I always supply my queries and programming codes as follows:

    SELECT FirstName FROM Students WHERE DoB >= '11 Dec 2016';
    

    Note also that Access will accept the #, thus:

    SELECT FirstName FROM Students WHERE DoB >= #11 Dec 2016#;
    

    but MS SQL server will not, so I always use " ' " as above, which both databases accept.

    And when getting that date from a variable in code, I always convert the result to string as follows:

    "SELECT FirstName FROM Students WHERE DoB >= " & myDate.ToString("d MMM yyyy")
    

    I am writing this because I know sometimes some programmers may not be keen enough to detect the inherent conversion. There will be no error for dates < 13, just different results!

    As for the question asked, add one day to the last date and make the comparison as follows:

    dated >= '11 Nov 2016' AND dated < '15 Nov 2016' 
    
    0 讨论(0)
  • 2020-11-22 09:51

    Really all sql dates should be in yyyy-MM-dd format for the most accurate results.

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