2016-05-12 90 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作爲日期),那麼在此期間應該沒有記錄。 –

相關問題