r/SQL • u/Glum-Molasses-4181 • 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
View all comments
Show parent comment
2
u/VladDBA SQL Server DBA May 07 '26
+1 for date table.
I've seen devs make queries perform terribly just because they were over-complicating things with nested UDFs (a UDF to figure out if it's a bank holiday, another to figure out if it's not during the weekend, another to get the fiscal year). Moving all that logic to a date table fixed those specific scenarios.