MSSQL DateTime Select

    and tagged as
  • sql

I’ve got a table with a ‘datetime’ field and I needed to retrieve rows from a specific year (2016) and month (December). After unsuccessfully trying a few of the usual WHERE LIKE '%2016-12%' statements a bit of research led me to the DATEPART function.

SELECT * FROM [TABLE] where (DATEPART(yy, [ColumnName]) = 2016 AND DATEPART(mm, [ColumnName]) = 12) 

A list of valid arguments can be found on MSDN