trevoray trevoray - 7 months ago 15
SQL Question

SQL Server to select conditional on what time of year it currently is

I could use assistance in figuring out how to write my SQL query to pull records conditionally based on the current month. I have a table of officer listings that only list the year in the start and end date.

So, say someone is listed as President from 2015-2016, and someone else is listed as Treasurer from 2017-2018. Each year in May, the new term of officers is listed. So, I can't just base the query based on current year.

So, if the date was April 15, 2016, this would display: John Smith, President (2015-2016)

If the date was May 1, 2016, this would display: Sally Sue, President (2016-2017)

SELECT Name, Position, StartTerm, EndTerm
FROM myTable
WHERE (if before may, show last year's terms, if after may show next year's terms)

Answer
SELECT Name, Position, StartTerm, EndTerm 
  FROM myTable 
 WHERE YearOfTerms = Year(DateAdd(mm, -4, GetDate()))