AnandPhadke
AnandPhadke

Reputation: 13486

Calculate the week number based on the specific start and end months, not the default SQL Server start month

In SQL Server as we know, it calculates the week number based on the Jan 1st of the current year till Dec 31st of the current year.

Now what I want is, I want to change these Jan 1st and Dec 31 to Mar 1 and Nov 30. Based on these SQL Server should calculate the week number.

So could anybody give me the SQL script for this?

Upvotes: 1

Views: 1697

Answers (2)

Yaroslav
Yaroslav

Reputation: 6534

I guess your question comes from the question posted earlier today about the week number on a fiscal year.

I found this link on the sqlTeam website, maybe a bit old but it explains very well the issues involved. After searching and reading a little bit more I have come to conclusion that the best approach is the one involving a calendar table as @AaronBertrand suggests

Upvotes: 0

Aaron Bertrand
Aaron Bertrand

Reputation: 280262

While you could create very complex expressions using dateadd and datepart, I'd suggest it is much simpler to create a calendar table. It could have a column representing your custom week number that you could calculate exactly one time for your entire range of dates, then it is always available in your queries and much easier to troubleshoot.

Upvotes: 5

Related Questions