SQL Server : fetching records between two dates?

前端 未结 6 919
暖寄归人
暖寄归人 2020-12-13 21:23

In SQL I write a SELECT statement to fetch data between two dates, using between and

Ex:

select * 
from xxx 
where dates b         


        
6条回答
  •  鱼传尺愫
    2020-12-13 21:49

    The unambiguous way to write this is (i.e. increase the 2nd date by 1 and make it <)

    select * 
    from xxx 
    where dates >= '20121026'
      and dates <  '20121028'
    

    If you're using SQL Server 2008 or above, you can safety CAST as DATE while retaining SARGability, e.g.

    select * 
    from xxx 
    where CAST(dates as DATE) between '20121026' and '20121027'
    

    This explicitly tells SQL Server that you are only interested in the DATE portion of the dates column for comparison against the BETWEEN range.

提交回复
热议问题