user5448913
user5448913

Reputation:

Extract the year out of a Date and compare it in the same query

I am working on a page that has to display lots of information depending on a year and a type filter. (I will use parametrized queries)

So my query should look something like :

SELECT Login FROM LeaveRequest 
WHERE StartDateYear = year 
  AND EndDateYear = year 
  AND Type = typeid

But here's the issue, my startDate and endDate are stored in the following format :

YYYY-MM-dd

Is there a way to extract the year out of these two and compare them with my year variable in the same query?

Upvotes: 0

Views: 50

Answers (2)

sreejithsdev
sreejithsdev

Reputation: 1210

SELECT Login FROM LeaveRequest 
WHERE StartDateYear = year(convert(date,'2015-11-18',111))
  AND EndDateYear = year(convert(date,'2015-11-18',111))
  AND Type = typeid

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172438

You can try like this using the Year function:

year(StartDateYear) = year AND year(EndDateYear) = year

Assuming the column as DateTime. Else you need to first cast the column as datetime

Upvotes: 1

Related Questions