SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 split year by week with start date and end date
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

eugz
Posting Yak Master

195 Posts

Posted - 11/28/2009 :  12:44:23  Show Profile  Reply with Quote
Hi All.
I would like create function or stored procedure to split year by week and display first date of the week and last date of the week. The week start from Sunday. For instance, result looks like:
2 | 1/4/2009 - 1/10/2009
If it possible not only for current year and for 5 years early and 5 years forward.
Thanks.

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/28/2009 :  13:40:02  Show Profile  Reply with Quote
do you mean this?

SELECT MIN(yourdatefield),MAX(yourdatefield)
FROM YourTable
GROUP BY DATEADD(wk,DATEDIFF(wk,0,yourdatefield),0)
Go to Top of Page

eugz
Posting Yak Master

195 Posts

Posted - 11/28/2009 :  18:19:03  Show Profile  Reply with Quote
I would like to get Year, # of week, first date of the week, and last date of the week. For 5 years early and 5 years forward from current, where week first day od week is Sunday. The source is CallDate datetime field. The result should looks like:

Year | #wk | Start week | End week
-------------------------------
...
2006 | 1 | 1/1/2006 | 1/7/2006
2006 | 2 | 1/8/2006 | 1/14/2006
...
and so on.

Thanks.
Go to Top of Page

Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)

USA
7020 Posts

Posted - 11/28/2009 :  18:39:24  Show Profile  Reply with Quote
You didn't explain how you want to handle the weeks when Jan 1 of a year is not Sunday. You will need to define that that, and how it will be handled.

Take a look at the function on the link below for generating a calendar table.
Date Table Function F_TABLE_DATE
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519



CODO ERGO SUM
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52325 Posts

Posted - 11/29/2009 :  12:41:12  Show Profile  Reply with Quote
quote:
Originally posted by eugz

I would like to get Year, # of week, first date of the week, and last date of the week. For 5 years early and 5 years forward from current, where week first day od week is Sunday. The source is CallDate datetime field. The result should looks like:

Year | #wk | Start week | End week
-------------------------------
...
2006 | 1 | 1/1/2006 | 1/7/2006
2006 | 2 | 1/8/2006 | 1/14/2006
...
and so on.

Thanks.



SELECT YEAR(Date),DATEPART(wk,Date),
MIN(Date) AS StartDate,
MAX(Date) AS EndDate
FROM table
WHERE Date >= DATEADD(yy,DATEDIFF(yy,0,GETDATE())-5,0)
AND Date < DATEADD(yy,DATEDIFF(yy,0,GETDATE())+5,0)+1
GROUP BY YEAR(Date),DATEPART(wk,Date)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000