无法从数据库中按日期获取记录

时间:2016-05-12 10:16:22

标签: sql-server database

enter image description here

我有基于列ID和日期的数据...日期的数据类型是DATETIME

DataBase:sql server 2008

查询1:

Select * 
from table t1 
where (t1.date between '2016-05-11' and  '2016-05-13')

查询2:

select * 
from table t1 
where t1.date IN ('2016-05-11')

结果 NUll 即使我在此日期有记录

除了(< = / < / > / > = )大于或小于

2 个答案:

答案 0 :(得分:0)

您可以使用以下查询,但您的查询也正确并且在我的服务器上运行。

Select * from table t1 
where t1.date between '20160511 00:00:00' and '20160513 23:59:59';

select * from table t1 where cast(t1.date as date) IN ('20160511');

答案 1 :(得分:0)

试试这个,

Select * 
from table t1 
where cast(t1.date as date) between '2016-05-11' and  '2016-05-13'

即将您的日期时间列转换为日期并进行比较,因为您的参数只是日期值。 或

where cast(t1.date as date) in ('2016-05-11')--For your second query

或使用大于或小于

Select * 
from table t1 
where t1.date >= '2016-05-11' and  t1.date < '2016-05-14'