Bluedog67

Random Thoughts About SQL Server and My Dog Blue

About Bluedog67

My name is Stephen Horne. I am a well-trained owner of an Australian Cattle Dog named Blue. I also develop software mainly using SQL Server, ASP.NET, and C#. I love data, databases, T-SQL, and turning raw data into actionable information. My plan for this blog is to write about SQL Server and sometimes my dog Blue. Please contact me at stephen at bluedog67 dot com. Follow me on Twitter at bluedog67.

Fiscal Week Scalar Value Function

This post is a quick and dirty solution to a question that came up on Twitter on the #sqlhelp hash tag from Barnaby Self (Blog|Twitter). @B4PJS wrote up a related blog post here. Please note that this possible solution has some potentially significant limitations: First, the possible solution assumes that the Fiscal Year begins on same date every year (some companies like Cisco have 52 week Fiscal Years). Second, the solution assumes that the Fiscal Year start date is Day 1 of Week 1 regardless of the day of the week. In other words, this solution is NOT affected by the SET DATEFIRST command. If SET DATEFIRST needs to impact Fiscal Week determination, this solution will need to be modified. Third, using scalar-valued UDFs (User Defined Functions) can cause significant performance problems if used in WHERE or GROUP BY clauses (nod to @DaveBally) - alternate solutions may yield better results. A possible better performing solution might be using the UDF described below in conjunction with either a persisted computed column or an indexed view.

-- Developed and tested only on SQL Server 2008 Dev Ed.
-- Assumes Fiscal Year begins July 1st.
-- Assumes day 1 of week 1 is July 1st regardless of day of week.
CREATE FUNCTION dbo.udf_GetFiscalYearWeek(@InputDate date)
RETURNS int
AS
BEGIN
  DECLARE @Result int
  
  DECLARE @FiscalYear int;
  -- Following logic will need to change if Fiscal Year start date different from YYYY0701.
  IF (MONTH(@InputDate) >= 7)
  BEGIN
    SET @FiscalYear = YEAR(@InputDate) + 1;
  END
  ELSE
  BEGIN
    SET @FiscalYear = YEAR(@InputDate);
  END

  DECLARE @FiscalYearStartDate date;
  -- Change '0701' as required.
  SET @FiscalYearStartDate = CAST((CAST((@FiscalYear - 1) AS varchar(4)) + '0701') AS DATE);

  DECLARE @FiscalYearWeek int;
  SET @FiscalYearWeek = DATEDIFF(d, @FiscalYearStartDate, @InputDate) / 7 + 1;

  SET @Result = @FiscalYearWeek;
  
  RETURN @Result
END
GO

Thanks for reading. Any feedback you have will be appreciated.

Categories: SQL Server
Permalink | Comments (7) | Post RSSRSS comment feed

Comments

Barnaby Self United Kingdom

Friday, January 15, 2010 8:56 AM

Barnaby Self

Cheers for the help. Very much appreciated

Stephen Horne (bluedog67) United States

Friday, January 15, 2010 9:29 AM

Stephen Horne (bluedog67)

Barnaby: Glad to help! Interesting problem. Stephen

topsy.com

Friday, January 15, 2010 12:10 PM

pingback

Pingback from topsy.com

Twitter Trackbacks for
        
        Bluedog67 | Fiscal Week Scalar Value Function
        [bluedog67.com]
        on Topsy.com

Barnaby Self United Kingdom

Saturday, January 16, 2010 2:32 PM

Barnaby Self

What still stuns me is that you posted your blog originally @ 0613 hrs! It was the middle of the day for me and my brain still was not in gear! BTW you are officially the first person to mention my Blog, so cheers for raising awareness of what should become a good source of help for people if I can get my act in gear! :-p

Barnaby Self United Kingdom

Saturday, January 16, 2010 2:33 PM

Barnaby Self

Sorry, wrong email address in previous post and Im trying to sign up for notifications!

stephen United States

Sunday, January 17, 2010 4:36 AM

stephen

Barnaby: I am in the same boat too. Just started the blog/twitter thing a few months ago myself. I have a long way to go! Again, interesting problem. I like concrete problems that I might know a solution to. Glad to help. Best wishes for your blog! Thanks, Stephen

uberVU - social comments

Sunday, January 17, 2010 10:35 AM

trackback

Social comments and analytics for this post

This post was mentioned on Twitter by bluedog67: @B4PJS Possible solution to Fiscal Week problem. http://bit.ly/69QNR4 Solution assumes July 1st is Day 1 of Week 1regardless of day of week.