Vijay Vijay - 25 days ago 9
SQL Question

Month & Year filter in SQL Server

I have a SQL Server table with following columns:

SQL Table

Now, I have to write query which will return me the records where "Month > May" and "F Year > 2016".

I am providing both "Month" (May) & "F Year" (2016) from my application. There are multiple records with the same Month & F Year column values.

I tried with Date part, Cast etc. but not getting required result.

Answer

You can do this (with month name)

SELECT *
FROM TableName
WHERE DATEPART(MM, ([Month] + ' 01 2016'))  > DATEPART(MM,'May' + ' 01 2016') 
  AND FYear > 2016

or that (with month number)

SELECT *
FROM TableName
WHERE DATEPART(MM, ([Month] + ' 01 2016'))  > 5
  AND FYear > 2016
Comments