简体   繁体   中英

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.

您应该在查询中添加以下内容:

YEAR(DateField) = 2015
Select * from YourTable where Year(SomeDateField)=2015

Select * from YourTable where Date(SomeDateField)>2015 And Date(SomeDateField)<2016; This helps find date between two years 2015 and 2016

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM