Reputation: 587
I would like to know if it's possible to have a query where the year of a date changes dynamically depending on the current year, with the month and the day remaining the same. For example, I would like the query to get the rows for which the date is between 2017-09-15 and 2018-03-15, if the current year is 2017.
Thanks in advance.
Upvotes: 0
Views: 356
Reputation: 82020
If 2012+, you can use datefromparts()
Example
Select DateR1 = DateFromParts(Year(GetDate()),9,15)
,DateR2 = DateAdd(MONTH,6,DateFromParts(Year(GetDate()),9,15))
Returns
DateR1 DateR2
2017-09-15 2018-03-15
So in a WHERE
...
Where DateCol between DateFromParts(Year(GetDate()),9,15)
and DateAdd(MONTH,6,DateFromParts(Year(GetDate()),9,15))
Upvotes: 6