-4

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

Nate
  • 61
  • 1
  • 3
  • 10
  • 1
    Please try something on your own first. This is an extremely basic query that is readily available on Google and in any SQL Server resource. – dfundako Sep 19 '16 at 17:48

4 Answers4

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
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 Year(SomeDateField)=2015
John Cappelletti
  • 79,615
  • 7
  • 44
  • 66
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