## Fiscal year week

 Author Message sathiyan00 SSC Eights! Group: General Forum Members Points: 937 Visits: 475 Hi all, I'm here with another question may be it is simple.but i searched in web i didn't get the correct answer...I want to populate fiscal year week in date dimension table.(from 1 April to 31 march)datepart(ww,date) will give week from 1 Jan.but i want for fiscal year.thanks and regardssathiyan laurie-789651 Hall of Fame Group: General Forum Members Points: 3606 Visits: 1272 Here's a possible solution:`-- Start & End of Fiscal Year:declare @StartDate Date, @DateInt int, @DateEndInt Int;set @StartDate = '01 Apr 2012'; -- Start Date of Fiscal Yearset @DateInt = datediff(dd, 0, @StartDate); -- Start Date as intset @DateEndInt = datediff(dd, 0, dateadd(yy, 1, @StartDate)); -- End Date as int (+1 day)-- Number of days from 1st Jan:declare @FirstDayOfYear Date, @DateOffset int;set @FirstDayOfYear = DATEADD(year,DATEDIFF(year,0,@StartDate),0); -- Date of First Day of Calendar Yearset @DateOffset = datediff(dd, @FirstDayOfYear, @StartDate) -- Difference between start of Calendar & Fiscal Years--print @DateInt--print @DateEndInt--print @DateShift-- Create date table with Date & Fiscal Week:declare @a as table ( [Date] Date, FiscalWeek Int );while @DateInt < @DateEndIntbegin -- Insert next Date, & week number subtracting offset to get correct fiscal week insert into @a values ( dateadd(dd, 0, @DateInt), datepart(ww, dateadd(dd, (-@DateOffset), @DateInt)) ); set @DateInt = @DateInt +1;endselect * from @a;` sathiyan00 SSC Eights! Group: General Forum Members Points: 937 Visits: 475 but for 2011 april first week starts from friday,saturday...using your query it gives like date week2011-04-01 12011-04-02 22011-04-03 22011-04-04 2this looks like it starts from saturday....any suggestions please.... laurie-789651 Hall of Fame Group: General Forum Members Points: 3606 Visits: 1272 Try this:`-- Start & End of Fiscal Year:declare @StartDate Date, @DateInt int, @DateEndInt Int;set @StartDate = '01 Apr 2012'; -- Start Date of Fiscal Yearset @DateInt = datediff(dd, 0, @StartDate); -- Start Date as intset @DateEndInt = datediff(dd, 0, dateadd(yy, 1, @StartDate)); -- End Date as int (+1 day)-- Create date table with Date & Fiscal Week:declare @a as table ( [Date] Date, DayName Varchar(10), FiscalWeek Int );declare @Start int;set @Start = @DateInt -- Populate table:while @DateInt < @DateEndIntbegin insert into @a values ( dateadd(dd, 0, @DateInt), DATENAME(weekday,dateadd(dd, 0, @DateInt)), ((@dateInt-@Start)/7)+1 ); set @DateInt = @DateInt +1;endselect * from @a;` sathiyan00 SSC Eights! Group: General Forum Members Points: 937 Visits: 475 thanks laurie, but this one also gives like:date day week1-Apr-2011 Friday 12-Apr-2011 Saturday 1 3-Apr-2011 Sunday 14-Apr-2011 Monday 15-Apr-2011 Tuesday 16-Apr-2011 Wednesday 17-Apr-2011 Thursday 18-Apr-2011 Friday 2But i want like...date day week1-Apr-2011 Friday 12-Apr-2011 Saturday 1 3-Apr-2011 Sunday 24-Apr-2011 Monday 25-Apr-2011 Tuesday 2 dwain.c SSC-Forever Group: General Forum Members Points: 44261 Visits: 6431 How about something like this?`DECLARE @StartDT DATETIME = '2011-04-01';WITH Dates AS ( SELECT TOP (1+DATEDIFF(day, @StartDT, DATEADD(day, -1, DATEADD(year, 1, @StartDT)))) d=DATEADD(day, ROW_NUMBER() OVER (ORDER BY (SELECT NULL))-1, @StartDT) FROM sys.all_columns)SELECT d, Day=DATENAME(dw, d) ,Week=CASE WHEN DATEPART(ww, d) <= 13 THEN 40+DATEPART(ww, d) ELSE DATEPART(ww, d) - 13 ENDFROM Dates` My mantra: No loops! No CURSORs! No RBAR! Hoo-uh!My thought question: Have you ever been told that your query runs too fast?My advice:INDEXing a poor-performing query is like putting sugar on cat food. Yeah, it probably tastes better but are you sure you want to eat it?The path of least resistance can be a slippery slope. Take care that fixing your fixes of fixes doesn't snowball and end up costing you more than fixing the root cause would have in the first place.Need to UNPIVOT? Why not CROSS APPLY VALUES instead?Since random numbers are too important to be left to chance, let's generate some!Learn to understand recursive CTEs by example.Splitting strings based on patterns can be fast!My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables