SQL 篩選問題

2007-07-17 1:45 pm
我想做日期既篩選,有無人知,做日期篩選既SQL係咩呢?
更新1:

例如係一張table度,我剩係要一月既記錄要點做呢?

更新2:

但我剩係想指定月份,即係2005、2006、2007既結果都有=.=

回答 (2)

2007-07-17 1:57 pm
✔ 最佳答案
select * from table1 where thedate between '200/1/1' and '2007/1/31'
or
select * from table1 where thedate >= '200/1/1' and thedate <= '2007/1/31'

For Access database, use # instead of single quote for date.

Beware of the trick, your date field may include the time part, in that case, the date range you select should be
thedate >= '2007/1/1/' and thedate < '2007/2/1'

because without specifying the time, 2007/1/31 is the same as 2007/1/31 12:00AM
The date that is 2007/1/31/ 13:00 will be excluded.

2007-07-17 06:01:27 補充:
Why are you not sleeping at this time of the day?

2007-07-17 06:06:04 補充:
What is your database platform?Month(thedate) = 1 Access, SQL Server, mysql

2007-07-17 23:08:28 補充:
[ 我剩係要一月既記錄要點做呢?但我剩係想指定月份,即係2005、2006、2007既結果都有]select * from table1 where MONTH(thedate) = 1

2007-07-17 23:10:11 補充:
universal 的日期格式為 m/d/yyyy, e.g. 1/31/2007
2007-07-17 5:14 pm
做日期既篩選即是在SQL加上限制日期的條件.

例如你有一table, 名稱為 order_table

最簡單的SQL去列出所有記錄, 可寫成
select * from order_table

結果為
Order_date
----------------
2006/12/29
2006/12/30
2006/12/31

原來這order_table 只有三個紀錄



若要只列出 2006/12/31 的 order 便要加入限制日期的條件 (假設table中 儲存order date 的欄位為order_date)
(注意:不同廠家/設定, 日期的格式或要寫成 31/12/2006 或 31-12-2006 或 2006-12-31)
select * from order_table where order_date = &#39;2006/12/31 &#39;

結果為
Order_date
----------------
2006/12/31

現只有一個紀錄


收錄日期: 2021-04-25 20:33:36
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20070717000051KK00542

檢視 Wayback Machine 備份