2012-08-03 59 views
8

我想寫select從數據庫中查找日期。 在數據庫中,我將字段actiondate作爲日期類型,日期類似於2012年7月12日17:21:33。 如何編寫select從7/12/2012獲取所有數據?sql date select

so if I have db like this: 
1 7/12/2012 17:21:33 
2 7/12/2012 15:21:35 
3 8/12/2012 8:25:35 
4 9/12/2014 8:25:35 

I want get only these rows: 
1 7/12/2012 17:21:33 
2 7/12/2012 15:21:35 
+2

你正在使用什麼數據庫軟件 – Dave 2012-08-03 14:57:32

回答

7
select * from 
yourtable where yourDateColumn < '2012-12-8' and yourDateColumn >= '2012-12-07' 
+2

也許「yourDateColumn> ='2012-12-07'」更好,如果你想趕上午夜也 – pater 2012-08-03 15:09:13

+0

謝謝!編輯 – 2012-08-03 15:09:48

1

您可以使用BETWEEN比較的日期:

select * 
from yourTable 
where yourDate between '2012-07-12' and '2012-08-12' 

或者,如果你正在尋找一個特別的日子,你可以使用。你可以改變,可以在被傳遞了「2012-07-12」的參數,所以你可以得到任何日期:

select * 
from yourTable 
where Cast(datediff(day, 0, yourDate) as datetime) = '2012-07-12' 
+0

問題是詢問一天;您正在提供一個月和一天的數據。 – 2012-08-03 15:31:47

+0

@JonathanLeffler編輯,包括他們是否只想要返回一天。 – Taryn 2012-08-03 15:36:00

2

我認爲朱利葉斯是問如何獲得只行7月12日/ 2012年17點21分33秒

所以我可以爲您提供一個解決方案,它被證明是正確的:

1)看看T-SQL轉換功能在T-SQL Convert function

2)當你將看看轉換碼

Convert codes

圖片來源:http://technet.microsoft.com/en-us/library/ms187928.aspx

,現在當我生氣大家對這裏的理論是代碼:

歐洲

select * from table_name 
where convert(varchar, column_that_has_date_in, 104) = '12.07.2012' 

對於美國

select * from table_name 
where convert(varchar, column_that_has_date_in, 101) = '07/12/2012' 

而另一件事值得一提的是,爲什麼當我們在SQL語句中鍵入我們得到0行:

select * from table_name 
where column_that_has_date_in like '%07/12/2012%' 

這是因爲該查詢正在尋找只爲沒有時間部分日期。