I am working on a SQL server and I have a list of dates in the format of 2015-03-17 how would I query the database so that only events that occurred in 2015 show up
Asked
Active
Viewed 249 times
4 Answers
3
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.

Community
- 1
- 1

Radu Gheorghiu
- 20,049
- 16
- 72
- 107
-
Got it, I see your update :). Deleted my comment and tossed in an upvote to get you positive. This is a nice answer. – JNevill Sep 19 '16 at 17:49
-
1
You should add this in your queries:
YEAR(DateField) = 2015

Rubens Farias
- 57,174
- 8
- 131
- 162
-
Actually, you shouldn't, and [**here's why**](http://stackoverflow.com/questions/799584/what-makes-a-sql-statement-sargable). – Radu Gheorghiu Nov 11 '16 at 08:49
0
Select * from YourTable where Date(SomeDateField)>2015 And Date(SomeDateField)<2016; This helps find date between two years 2015 and 2016

Black Mamba
- 13,632
- 6
- 82
- 105
-
-
He has explicitly given date so date can't be taken directly 2015. – Black Mamba Sep 19 '16 at 17:53