Datetime in where clause

前端 未结 6 883
终归单人心
终归单人心 2020-12-13 08:15

How can I select 12/20/2008 in where clause of sql?

The server is SQL server 2005.

select * from tblErrorLog
where errorDate = \'12/20/2         


        
6条回答
  •  生来不讨喜
    2020-12-13 08:54

    Assuming we're talking SQL Server DateTime

    Note: BETWEEN includes both ends of the range, so technically this pattern will be wrong:

    errorDate BETWEEN '12/20/2008' AND '12/21/2008'
    

    My preferred method for a time range like that is:

    '20081220' <= errorDate AND errordate < '20081221'
    

    Works with common indexes (range scan, SARGable, functionless) and correctly clips off midnight of the next day, without relying on SQL Server's time granularity (e.g. 23:59:59.997)

提交回复
热议问题