What is the optimal way to compare dates in Microsoft SQL server?

前端 未结 4 1382
别跟我提以往
别跟我提以往 2020-12-25 11:27

I have a SQL datetime field in a very large table. It\'s indexed and needs to be queried.

The problem is that SQL always stores the time component (even

4条回答
  •  自闭症患者
    2020-12-25 12:19

    Here is an example:

    I've an Order table with a DateTime field called OrderDate. I want to retrieve all orders where the order date is equals to 01/01/2006. there are next ways to do it:

    1) WHERE DateDiff(dd, OrderDate, '01/01/2006') = 0
    2) WHERE Convert(varchar(20), OrderDate, 101) = '01/01/2006'
    3) WHERE Year(OrderDate) = 2006 AND Month(OrderDate) = 1 and Day(OrderDate)=1
    4) WHERE OrderDate LIKE '01/01/2006%'
    5) WHERE OrderDate >= '01/01/2006'  AND OrderDate < '01/02/2006'
    

    Is found here

提交回复
热议问题