[英]SQL Year Date Query
我正在使用 SQL 服务器,我有一个格式为 2015-03-17 的日期列表,我将如何查询数据库,以便仅显示 2015 年发生的事件
You might get better results by trying:您可能会通过尝试获得更好的结果:
SELECT *
FROM TABLE
WHERE DateField >= '2015-01-01 00:00:00'
and DateField < '2016-01-01 00:00:00'
This has a higher chance of being " sargable ", if you have a index on that column, rather than applying a function over your DateField
, which will most certainly bypass using the index.如果您在该列上有一个索引,而不是在DateField
应用一个函数,这将更有可能成为“ sargable ”,这肯定会绕过使用索引。
您应该在查询中添加以下内容:
YEAR(DateField) = 2015
Select * from YourTable where Year(SomeDateField)=2015
Select * from YourTable where Date(SomeDateField)>2015 And Date(SomeDateField)<2016; Select * from YourTable where Date(SomeDateField)>2015 And Date(SomeDateField)<2016; This helps find date between two years 2015 and 2016这有助于找到 2015 年和 2016 年之间的日期
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.