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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 How to calculate weeks

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 that


Week-Start Week-End Week-NO

01-Jan-2010 03-Jan-2010 01
04-Jan-2010 10-Jan-2010 02
11-Jan-2010 17-Jan-2010 03

and 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_DATE
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519

Look at these columns:
START_OF_WEEK_STARTING_MON_DATE
First Day of Week starting Monday that DATE is in
END_OF_WEEK_STARTING_MON_DATE
Last Day of Week starting Monday that DATE is in


You didn't really define or give enouigh examples of the week number to know how to do that.




CODO ERGO SUM
Go to Top of Page

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 DATETIME
DECLARE @Count INT SET @Count = 1

DECLARE @Results TABLE (
StartDate DATETIME,
EndDate DATETIME,
WeekNo INT
)

SET @d = @Start
WHILE DATEPART(dw, @d) != 1
SET @d = DATEADD(d, 1, @d)

WHILE @Start <= @End
BEGIN
INSERT INTO @Results
SELECT @Start, @d, @Count

SET @Start = DATEADD(d, 1, @d)
SET @d = DATEADD(wk, 1, @d)
SET @Count = @Count + 1
END

SELECT * FROM @Results


------------------------------------------------------------------------------------
Any and all code contained within this post comes with a 100% money back guarantee.
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-04-09 : 09:50:20
[code]
declare @tbl as table(stdt datetime,enddt datetime)
insert into @tbl
select '01-Jan-2010','30-April-2010'
;with cte
as
(

select stdt,enddt,stdt as stweek from @tbl t1
union all
select t2.stdt,t2.enddt,DATEADD(wk,1,c1.stweek)from @tbl t2
inner 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
Go to Top of Page

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 @tbl
select '01-Jan-2010','30-April-2010'
;with cte
as
(

select stdt,enddt,stdt as stweek from @tbl t1
union all
select t2.stdt,t2.enddt,DATEADD(wk,1,c1.stweek)from @tbl t2
inner 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.
Go to Top of Page

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
Go to Top of Page

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.
Go to Top of Page
   

- Advertisement -