Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Fiscal year week Expand / Collapse
Author
Message
Posted Wednesday, September 26, 2012 12:03 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, May 15, 2014 2:05 PM
Points: 73, 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 regards
sathiyan
Post #1364446
Posted Wednesday, September 26, 2012 3:34 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 320, Visits: 1,079
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 Year
set @DateInt = datediff(dd, 0, @StartDate); -- Start Date as int
set @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 Year
set @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 < @DateEndInt
begin
-- 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;
end

select * from @a;


Post #1364525
Posted Wednesday, September 26, 2012 5:42 AM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, May 15, 2014 2:05 PM
Points: 73, Visits: 475
but for 2011 april first week starts from friday,saturday...
using your query it gives like
date week
2011-04-01 1
2011-04-02 2
2011-04-03 2
2011-04-04 2


this looks like it starts from saturday....
any suggestions please....
Post #1364583
Posted Wednesday, September 26, 2012 6:26 AM


Old Hand

Old HandOld HandOld HandOld HandOld HandOld HandOld HandOld Hand

Group: General Forum Members
Last Login: Friday, June 27, 2014 8:03 AM
Points: 320, Visits: 1,079
Try this:

-- Start & End of Fiscal Year:
declare @StartDate Date, @DateInt int, @DateEndInt Int;
set @StartDate = '01 Apr 2012'; -- Start Date of Fiscal Year
set @DateInt = datediff(dd, 0, @StartDate); -- Start Date as int
set @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 < @DateEndInt
begin
insert into @a values ( dateadd(dd, 0, @DateInt), DATENAME(weekday,dateadd(dd, 0, @DateInt)), ((@dateInt-@Start)/7)+1 );
set @DateInt = @DateInt +1;
end

select * from @a;


Post #1364616
Posted Wednesday, September 26, 2012 12:35 PM
Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: Thursday, May 15, 2014 2:05 PM
Points: 73, Visits: 475
thanks laurie,
but this one also gives like:
date day week
1-Apr-2011 Friday 1
2-Apr-2011 Saturday 1
3-Apr-2011 Sunday 1
4-Apr-2011 Monday 1
5-Apr-2011 Tuesday 1
6-Apr-2011 Wednesday 1
7-Apr-2011 Thursday 1
8-Apr-2011 Friday 2


But i want like...
date day week
1-Apr-2011 Friday 1
2-Apr-2011 Saturday 1
3-Apr-2011 Sunday 2
4-Apr-2011 Monday 2
5-Apr-2011 Tuesday 2
Post #1364885
Posted Wednesday, September 26, 2012 7:29 PM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 8:47 PM
Points: 3,648, Visits: 5,318
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 END
FROM 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!
Post #1364997
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse