Solved

Grouping dates by week

Posted on 2012-03-26
3
417 Views
Last Modified: 2012-03-27
I have a field called FieldA which has a date value.  I would like to group the dates by week. Week1 - Week52.  

What syntax can I use to assign a week number to each respective date.
We will use Sunday as the first day of the week and Saturday as the last day of the week.
0
Comment
Question by:morinia
3 Comments
 
LVL 16

Expert Comment

by:Wasim Akram Shaik
ID: 37768735
you can use this for getting the week number

select to_char(sysdate,'IW') from dual;

and use the same thing in group by also..
0
 
LVL 16

Expert Comment

by:DcpKing
ID: 37769411
Assuming SQL Server (for Oracle see previous answer):

select datepart(week, @dtDateValue) will return an integer corresponding to the ISO week number (week 1 is the week containing the first Thursday in the year; weeks start on Sunday).

Thus try grouping on datepart(week, FieldA):

select datepart(week, FieldA) as 'WeekNr', OtherField1, OtherField2, .....
from TableName
group by datepart(week, FieldA)

should give you the results with the week number at the front.

(Apologies for any typos - I don't have a copy of SQL Server in front of me to check things with)

Mike
0
 
LVL 11

Accepted Solution

by:
theartfuldazzler earned 500 total points
ID: 37769958
Hi Morinia

I know you have asked SAS questions before, so here is the SAS solution:

DATA Input_DS;
  Date = '01Jan2012'd;
  OUTPUT;
  DATE = '31Dec2011'd;
  OUTPUT;
  DATE = '29JUL2009'd;
  OUTPUT;
  FORMAT Date Date9.;
RUN;

DATA Output_DS;
  Set Input_DS;
    Start_of_year = MDY(1,1, YEAR(Date));
	Week_no = INTCK("WEEK",Start_of_year,Date)+1;  
	/* One might need to make adjustments to the +1 depending on how one counts the first week */
	DROP Start_of_year;
RUN;

Open in new window

0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now