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 2000 Forums
 Transact-SQL (2000)
 End of Week Calculation

Author  Topic 

mdelgado
Posting Yak Master

141 Posts

Posted - 2003-01-02 : 16:38:35
Happy New Year to all...

I'm currently working on a sproc that will Email out totals by week.

I need to write something that will calculate the end of each week for 3 weeks going back. For example,

Last week - 12/22 - 12/28
2 weeks back - 12/15 - 12/21
3 weeks back - 12/8 - 12/14

Any ideas?

thanks.

Page47
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2003-01-03 : 07:08:15
What have you tried so far? Have you looked at DATEPART, DATEDIFF and the other functions for working with datetime datatypes?

Jay White
{0}
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2003-01-03 : 09:38:18
Here's something you can play with, that should give you the answer:

----

declare @startdate datetime;

declare @WeekBegin datetime;
declare @WeekEnd datetime;

set @startdate = '1/3/2003' /* sample starting date */

set @weekEnd = dateadd(d,-1 * datepart(dw,@startdate),@startDate)
set @weekBegin = @weekEnd - 6

print 'last week:'
print @weekbegin
print @weekend
print ''
print '2 weeks back:'
print dateadd(d,-7,@weekbegin)
print dateadd(d,-7,@weekend)
print ''
print '3 weeks back:'
print dateadd(d,-14,@weekbegin)
print dateadd(d,-14,@weekend)

--------

- Jeff
Go to Top of Page

mdelgado
Posting Yak Master

141 Posts

Posted - 2003-01-03 : 10:23:17
That will work. Thanks!!

Go to Top of Page
   

- Advertisement -