Solved

Grouping dates by week

Posted on 2012-03-26
3
457 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I annotated my article on ransomware somewhat extensively, but I keep adding new references and wanted to put a link to the reference library.  Despite all the reference tools I have on hand, it was not easy to find a way to do this easily. I finall…
These days, all we hear about hacktivists took down so and so websites and retrieved thousands of user’s data. One of the techniques to get unauthorized access to database is by performing SQL injection. This article is quite lengthy which gives bas…
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…

726 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