2016-05-12 62 views
0

enter image description here不能通过日期从数据库中获取记录

我基于列ID和日期有数据......数据类型,日期为DATETIME

数据库: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即使我有在这个日期的记录

那么,有任何替代获取记录除了< =/</>/> =)更大的比或小于

+0

请用select * from t1表,其中 '2016年5月13日' '2016年5月11日' 之间(t1.date和) –

+0

参考http://stackoverflow.com/questions/10643379/how-do-i-query-for-all-dates-greater-than-a-certain-date-in-sql-server – wajeeh

+0

@ChetanSanghani您的查询是查询1,我没有得到任何记录 –

回答

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'); 
+0

它正在工作,如果我有数据库中的数据类型“日期”....但不适用于Da ta输入“datetime” –

+0

您正在使用哪个服务器版本? –

+0

sql server 2008 R2 –

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' 
+0

@TomTom:这个答案呢? – Wanderer

+0

@Abdul Rasheed,如果我在数据库中有数据类型“Date”,但没有使用数据类型“datetime”,则铸造查询正在工作。 –

+0

如果您没有得到查询结果 - 在2016-05-11和2016-05-13之间施加了(t1.date作为日期),那么在此期间应该没有记录。 –

相关问题