r/SQL • • May 07 '26

SQL Server SQL2018 - variable date ranges

Hi - I need help with a variable date range formula. I need to capture data from Jun 1 of the previous year through May 31 of current year. The query needs to be able to run anytime through the year and needs to work in future years. So this year it would be 6/1/25 -5/31/26, but next year would be 6/1/26-5/31/27. I can't figure how without hardcoding the dates. I found a formula but it only works with earlier versions of sql. Can anyone assist?

0 Upvotes

7 comments sorted by

View all comments

Show parent comment

2

u/Dead_Parrot May 07 '26 edited May 07 '26

Yeah calendar table works a dream. Also really useful for adding local holidays and the likes

https://github.com/James-Poland/ABetterCalender/blob/main/calendar%20set%20up.sql

Here's one I've used in the past.... Made for irish market