MS Access SQL to Select date range

情到浓时终转凉″ 提交于 2019-12-24 13:15:49

问题


I need to select a record with dates which has dates ( in range: form 1998 to 1999). I wrote the statement which did seem to work . Why?

SELECT *
FROM Factory
WHERE 
(EXTRACT(YEAR FROM date) AS dyear) BETWEEN '1998'  AND '1999'

回答1:


You can use YEAR() to get the year from the date.

SELECT *
FROM   Factory
WHERE  YEAR(date) BETWEEN 1998 AND 1999
  • MSAccess YEAR()



回答2:


Applying the Year() function for every row in Factory will be a noticeable performance challenge if the table includes thousands of rows. (Actually it would be a performance challenge for a smaller table, too, but you would be less likely to notice the hit in that case.) A more efficient approach would be to index the [date] field and use indexed retrieval to limit the db engine's workload.

SELECT f.*
FROM Factory AS f
WHERE f.date >= #1998-1-1# AND f.date < #2000-1-1#;

Whenever possible, design your queries to take advantage of indexed retrieval. That can improve performance dramatically. As a simplistic rule of thumb: indexed retrieval = good; full table scan = bad. Try to avoid full tables scans whenever possible.



来源:https://stackoverflow.com/questions/15790213/ms-access-sql-to-select-date-range

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!