Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
imughal
Posting Yak Master
192 Posts |
Posted - 2010-04-09 : 09:03:05
|
| Hi,how do I calculate week start and end in a given period. For example I have enter 01-Jan-2010 – 30-April-2010, and calculate weeks. Result should be like thatWeek-Start Week-End Week-NO01-Jan-2010 03-Jan-2010 0104-Jan-2010 10-Jan-2010 0211-Jan-2010 17-Jan-2010 03and so on till given end date. Want to calculate week from Monday – Sunday. Kindly guide how to get this result.Thx |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2010-04-09 : 09:31:27
|
| The function on this like will give you the start and end date for each week.Date Table Function F_TABLE_DATEhttp://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519Look at these columns:START_OF_WEEK_STARTING_MON_DATE First Day of Week starting Monday that DATE is inEND_OF_WEEK_STARTING_MON_DATE Last Day of Week starting Monday that DATE is inYou didn't really define or give enouigh examples of the week number to know how to do that.CODO ERGO SUM |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-09 : 09:45:42
|
Here's a script that populates a table variable with the start/end dates and the week no for each week in the given period, if that's what you're after:DECLARE @Start DATETIME SET @Start = '20100101'DECLARE @End DATETIME SET @End = '20100430'DECLARE @d DATETIMEDECLARE @Count INT SET @Count = 1DECLARE @Results TABLE ( StartDate DATETIME, EndDate DATETIME, WeekNo INT)SET @d = @StartWHILE DATEPART(dw, @d) != 1 SET @d = DATEADD(d, 1, @d)WHILE @Start <= @EndBEGIN INSERT INTO @Results SELECT @Start, @d, @Count SET @Start = DATEADD(d, 1, @d) SET @d = DATEADD(wk, 1, @d) SET @Count = @Count + 1ENDSELECT * FROM @Results ------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-09 : 09:50:20
|
| [code]declare @tbl as table(stdt datetime,enddt datetime)insert into @tblselect '01-Jan-2010','30-April-2010' ;with cteas(select stdt,enddt,stdt as stweek from @tbl t1union allselect t2.stdt,t2.enddt,DATEADD(wk,1,c1.stweek)from @tbl t2inner join cte c1 on DATEADD(wk,1,c1.stweek)<t2.enddt) select stweek as startweek,DATEADD(wk,1,stweek)as endweek,ROW_NUMBER()over(order by (select 1))as weekno from cte[/code]PBUH |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-09 : 09:53:51
|
quote: Originally posted by Idera
declare @tbl as table(stdt datetime,enddt datetime)insert into @tblselect '01-Jan-2010','30-April-2010' ;with cteas(select stdt,enddt,stdt as stweek from @tbl t1union allselect t2.stdt,t2.enddt,DATEADD(wk,1,c1.stweek)from @tbl t2inner join cte c1 on DATEADD(wk,1,c1.stweek)<t2.enddt) select stweek as startweek,DATEADD(wk,1,stweek)as endweek,ROW_NUMBER()over(order by (select 1))as weekno from cte
That gives Friday to Thursday, not Monday to Sunday. That's because 1 Jan 2010 is a Friday. You need to account for the fact that the first week will be less than 7 days long.However, I do like the way you did it in a select query rather than a loop.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
Sachin.Nand
2937 Posts |
Posted - 2010-04-09 : 09:58:55
|
| Well it depends upon your server setting on how you define your startweek day try this select @@DATEFIRST.You can set your startweek from 1 to 7 with function set datefirst 'Your day number from 1 to 7'PBUH |
 |
|
|
DBA in the making
Aged Yak Warrior
638 Posts |
Posted - 2010-04-09 : 10:06:22
|
quote: Originally posted by Idera Well it depends upon your server setting on how you define your startweek day try this select @@DATEFIRST.You can set your startweek from 1 to 7 with function set datefirst 'Your day number from 1 to 7'
When I tested your query, it gave the same result regardless of set datefirst. Run it yourself with various set datefirst values, and compare the output with the requested output in the OP. It's not the same. It assume that the second week begins 7 days after the startdate, when the requirement is that the second week begins on the first Monday after the startdate.------------------------------------------------------------------------------------Any and all code contained within this post comes with a 100% money back guarantee. |
 |
|
|
|
|
|
|
|