根据MSSQL中的日期数据

时间:2016-06-22 04:57:26

标签: sql-server sql-server-2008 select

我有这样的数据

 RID      Region        StartDate                EndDate
944     Canada      2016-01-09 00:00:00.000 2016-01-16 23:59:59.000
955     Canada      2016-01-17 00:00:00.000 2016-01-24 23:59:59.000
981     Canada      2016-02-01 00:00:00.000 2016-02-08 23:59:59.000
996     Canada      2016-02-09 00:00:00.000 2016-02-16 23:59:59.000
1006    Canada      2016-01-25 00:00:00.000 2016-01-31 23:59:59.000
1020    Canada      2016-02-17 00:00:00.000 2016-02-24 23:59:59.000
1030    Canada      2016-02-25 00:00:00.000 2016-02-29 23:59:59.000
1041    Canada      2016-03-01 00:00:00.000 2016-03-08 23:59:59.000
1046    Canada      2016-03-09 00:00:00.000 2016-03-16 23:59:59.000
1062    Canada      2016-03-17 00:00:00.000 2016-03-24 23:59:59.000
1073    Canada      2016-03-24 00:00:00.000 2016-03-31 23:59:59.000
1083    Canada      2016-04-01 00:00:00.000 2016-04-08 23:59:59.000
1105    Canada      2016-04-09 00:00:00.000 2016-04-16 23:59:59.000
1118    Canada      2016-04-17 00:00:00.000 2016-04-24 23:59:59.000
1128    Canada      2016-04-25 00:00:00.000 2016-04-30 23:59:59.000
1164    Canada      2016-05-01 00:00:00.000 2016-05-08 23:59:59.000

现在我尝试选择这样的数据

select * from tab1 where Region='Canada'
and StartDate ='2016-01-09 00:00:00.000'
and EndDate ='2016-01-24 23:59:59.000'

期望的结果是

RID      Region     StartDate                EndDate
944     Canada      2016-01-09 00:00:00.000 2016-01-16 23:59:59.000
955     Canada      2016-01-17 00:00:00.000 2016-01-24 23:59:59.000

但是当我执行此查询时,数据为空

任何解决方案?

3 个答案:

答案 0 :(得分:3)

我认为你打算限制日期范围,但实际上你只限制了两个时间点。试试这个问题:

SELECT *
FROM tab1
WHERE Region = 'Canada' AND
      StartDate >= '2016-01-09 00:00:00.000' AND
      EndDate <= '2016-01-24 23:59:59.000'

答案 1 :(得分:0)

试试这个。

SELECT * 
FROM tab1 
WHERE Region = 'Canada'
    AND StartDate >='2016-01-09 00:00:00.000'
    AND EndDate <='2016-01-24 23:59:59.000'

答案 2 :(得分:0)

&#39;之间的关系必须工作。我试过这个。如果它不起作用,请尝试转换那些日期时间列的函数。

SELECT *
FROM tab1
WHERE Region = 'Canada' AND
StartDate >= convert(datetime,'2016-01-09 00:00:00.000') AND
EndDate <= convert(datetime,'2016-01-24 23:59:59.000')