how to query where date with time = date without time in ms sql

前端 未结 4 2085
臣服心动
臣服心动 2021-02-06 02:33

I want to do a query with dates this is my sample tsql:

select * from Bookings where StartTime = \'2/15/2014\'

the starttime has value \'2/15/2

4条回答
  •  半阙折子戏
    2021-02-06 02:52

    The best way to do this is with a simple comparison:

    select *
    from Bookings
    where StartTime >= cast('2014-02-15' as date) and StartTime < cast('2014-02-14' as date);
    

    This is the safest method of comparison, because it will take advantage of an index on StartTime. This property is called "sargability".

    In SQL Server, casting to a date should also be sargable, so you could also do:

    select *
    from Bookings
    where cast(StartTime as date) = cast('2014-02-15' as date) ;
    

提交回复
热议问题