SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Fiscal year week


Fiscal year week

Author
Message
sathiyan00
sathiyan00
Old Hand
Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)

Group: General Forum Members
Points: 373 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
laurie-789651
laurie-789651
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1508 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 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;



sathiyan00
sathiyan00
Old Hand
Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)

Group: General Forum Members
Points: 373 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....
laurie-789651
laurie-789651
SSCommitted
SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)SSCommitted (1.5K reputation)

Group: General Forum Members
Points: 1508 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 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;



sathiyan00
sathiyan00
Old Hand
Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)Old Hand (373 reputation)

Group: General Forum Members
Points: 373 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
dwain.c
dwain.c
SSCoach
SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)SSCoach (18K reputation)

Group: General Forum Members
Points: 18343 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 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!
My temporal SQL musings: Calendar Tables, an Easter SQL, Time Slots and Self-maintaining, Contiguous Effective Dates in Temporal Tables
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search