SQL query to select dates between two dates

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

    Check below Examples: Both working and Non-Working.

    select * from tblUser Where    
    convert(varchar(10),CreatedDate,111) between '2015/04/01' and '2016/04/01' //--**Working**
    

    OR

    select * from tblUser Where
    (CAST(CreatedDate AS DATETIME) between CAST('2015/04/01' AS DATETIME) And CAST('2016/4/30'AS DATETIME)) //--**Working**
    

    OR

    select * from tblUser Where
    (YEAR(CreatedDate) between YEAR('2015/04/01') And YEAR('2016/4/30')) 
    //--**Working**
    

    AND below is not working:

    select * from tblUser Where
    Convert(Varchar(10),CreatedDate,111) >=  Convert(Varchar(10),'01-01-2015',111) and  Convert(Varchar(10),CreatedDate,111) <= Convert(Varchar(10),'31-12-2015',111) //--**Not Working**
    
    
    select * from tblUser Where
    (Convert(Varchar(10),CreatedDate,111) between Convert(Varchar(10),'01-01-2015',111) And Convert(Varchar(10),'31-12-2015',111)) //--**Not Working**
    
    0 讨论(0)
  • 2020-11-22 09:36

    There are a lot of bad answers and habits in this thread, when it comes to selecting based on a date range where the records might have non-zero time values - including the second highest answer at time of writing.

    Never use code like this: Date between '2011/02/25' and '2011/02/27 23:59:59.999'

    Or this: Date >= '2011/02/25' and Date <= '2011/02/27 23:59:59.999'

    To see why, try it yourself:

    DECLARE @DatetimeValues TABLE
        (MyDatetime datetime);
    INSERT INTO @DatetimeValues VALUES
        ('2011-02-27T23:59:59.997')
        ,('2011-02-28T00:00:00');
    
    SELECT MyDatetime
    FROM @DatetimeValues
    WHERE MyDatetime BETWEEN '2020-01-01T00:00:00' AND '2020-01-01T23:59:59.999';
    
    SELECT MyDatetime
    FROM @DatetimeValues
    WHERE MyDatetime >= '2011-02-25T00:00:00' AND MyDatetime <= '2011-02-27T23:59:59.999';
    

    In both cases, you'll get both rows back. Assuming the date values you're looking at are in the old datetime type, a date literal with a millisecond value of 999 used in a comparison with those dates will be rounded to millisecond 000 of the next second, as datetime isn't precise to the nearest millisecond. You can have 997 or 000, but nothing in between.

    You could use the millisecond value of 997, and that would work - assuming you only ever need to work with datetime values, and not datetime2 values, as these can be far more precise. In that scenario, you would then miss records with a time value 23:59:59.99872, for example. The code originally suggested would also miss records with a time value of 23:59:59.9995, for example.

    Far better is the other solution offered in the same answer - Date >= '2011/02/25' and Date < '2011/02/28'. Here, it doesn't matter whether you're looking at datetime or datetime2 columns, this will work regardless.

    The other key point I'd like to raise is date and time literals. '2011/02/25' is not a good idea - depending on the settings of the system you're working in this could throw an error, as there's no 25th month. Use a literal format that works for all locality and language settings, e.g. '2011-02-25T00:00:00'.

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

    You ca try this SQL

    select * from employee where rec_date between '2017-09-01' and '2017-09-11' 
    
    0 讨论(0)
  • 2020-11-22 09:37

    I like to use the syntax '1 MonthName 2015' for dates ex:

       WHERE aa.AuditDate>='1 September 2015'
         AND aa.AuditDate<='30 September 2015'
    

    for dates

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

    This query stands good for fetching the values between current date and its next 3 dates

    SELECT * FROM tableName  WHERE columName 
    BETWEEN CURDATE() AND DATE_ADD(CURDATE(), INTERVAL 3 DAY)
    

    This will eventually add extra 3 days of buffer to the current date.

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

    it's better write this way:

    CREATE PROCEDURE dbo.Get_Data_By_Dates
    (
        @EmployeeId INT = 1,
        @Start_Date DATE,
        @End_Date Date
    )
    AS
    Select * FROM Calculation  
        where EmployeeId=@EmployeeId AND Test_Date BETWEEN @Start_Date AND @End_Date
    RETURN
    
    0 讨论(0)
提交回复
热议问题