日期时间比较操作

时间:2014-07-17 21:54:14

标签: sql tsql datetime sql-server-2012

我在我的数据库中有这些记录:

enter image description here

date

中的datetime列类型

我还有一个查询从表中提取一些记录:

SELECT count(*) 
FROM Availibility 
WHERE [date]>= @StartDate AND [date]<=@EndDate

这是结果的屏幕截图:

enter image description here

我预计会有Count(*)= 5但我得到4! 我的错是什么?

更新

SELECT COUNT(*)更改为SELECT *

enter image description here

这些是最新记录:

enter image description here

另外,如果我从58改为56,我得到了5个结果!!!

尝试通过此查询重建索引:

alter index all on Availibility rebuild

但结果是一样的,我有几秒钟的4条记录。

1 个答案:

答案 0 :(得分:0)

测试代码:

create table Availability
(
  [date] datetime not null
)

insert Availability ([date]) select '2014-07-19 02:02:57.000'
union select '2014-07-19 02:03:57.000'
union select '2014-07-19 02:04:57.000'
union select '2014-07-19 02:05:57.000'
union select '2014-07-19 02:06:57.000'
union select '2014-07-19 02:07:57.000'
union select '2014-07-19 02:08:57.000'
union select '2014-07-19 02:09:57.000'
union select '2014-07-19 02:10:57.000'
union select '2014-07-19 02:11:57.000'
union select '2014-07-19 02:12:57.000'

declare @startDate datetime = '2014-07-19 02:07:58.000'
, @endDate datetime = '2014-07-19 02:12:58.000'

select * 
from Availability
where [date] between @startDate and @endDate