Generating 52 weeks number of a year associated to a date

hi
I try to generate the 52 weeks number of a year associated to the date of the first monday of the week
For example 1 (week number 1) would be associated to the first monday on the first week of the year.
Week 2 associated to the secondth week of the year, adn so on. The result would be to columns :
Number of the wee and its corresponding date of the monday :
Number_of_the_week----DATE
1----------------------------01/01/2008
2----------------------------14/01/2008
...... etc....
52---------------------------29/12/2008
The request should be something including the year in a "select .(something)... from dual"
Could you suggest me something ?
Thank you for your kind answers !Christian

Christian from France wrote:
It is not so bad except that each date falls on the tuesday instead of the monday of the week. Hmmm, I don't remember you specifying that in your initial requirements.
SQL> ed
Wrote file afiedt.buf
  1  with t as (select 2009 as yr from dual)
  2      ,off as (select to_number(to_char(trunc(to_date(yr,'YYYY'),'YYYY'),'D'))-1 as offset from t)
  3  select rownum as week, trunc(to_date(t.yr,'YYYY'),'YYYY')+((rownum-1)*7)-decode(rownum,1,0,off.offset) as dt
  4  from t, off
  5* connect by rownum <= 53
SQL> /
      WEEK DT
         1 01-JAN-09
         2 05-JAN-09
         3 12-JAN-09
         4 19-JAN-09
         5 26-JAN-09
         6 02-FEB-09
         7 09-FEB-09
         8 16-FEB-09
         9 23-FEB-09
        10 02-MAR-09
        11 09-MAR-09
        12 16-MAR-09
        13 23-MAR-09
        14 30-MAR-09
        15 06-APR-09
        16 13-APR-09
        17 20-APR-09
        18 27-APR-09
        19 04-MAY-09
        20 11-MAY-09
        21 18-MAY-09
        22 25-MAY-09
        23 01-JUN-09
        24 08-JUN-09
        25 15-JUN-09
        26 22-JUN-09
        27 29-JUN-09
        28 06-JUL-09
        29 13-JUL-09
        30 20-JUL-09
        31 27-JUL-09
        32 03-AUG-09
        33 10-AUG-09
        34 17-AUG-09
        35 24-AUG-09
        36 31-AUG-09
        37 07-SEP-09
        38 14-SEP-09
        39 21-SEP-09
        40 28-SEP-09
        41 05-OCT-09
        42 12-OCT-09
        43 19-OCT-09
        44 26-OCT-09
        45 02-NOV-09
        46 09-NOV-09
        47 16-NOV-09
        48 23-NOV-09
        49 30-NOV-09
        50 07-DEC-09
        51 14-DEC-09
        52 21-DEC-09
        53 28-DEC-09
53 rows selected.
SQL>

Similar Messages

  • How to get a week number  for the year using oracle sql query?

    hi everyone,
    i have the requirement to find the week number for the calender..
    so that week number should start with 01 when the year starts and it should end with week end date(that is first saturday of the january month).. so next week number starts with sunday and ends with saturday ,continously.. in the end date of the year it should not be 'saturday' but week number should end with last date of the year.. again in the next year it should start with '01'.
    for example:
    01-JAN-13 tuesday 01
    02-JAN-13 wednesday 01
    03-JAN-13 thursday 01
    04-JAN-13 friday 01
    05-JAN-13 saturday 01
    06-JAN-13 sunday 02
    07-JAN-13 monday 02
    26-DEC-13 thursday 52
    27-DEC-13 friday 52
    28-DEC-13 saturday 52
    29-DEC-13 sunday 53
    30-DEC-13 monday 53
    31-DEC-13 tuesday 53
    01-JAN-14 wednesday 01
    02-JAN-14 thursday 01
    how can i achieve this, can anyone please help me out on this..
    i have a query that starts with 01 when year starts but it gives problem in the end of the year .. described below with a query..
    select mydate,
    to_char(mydate,'day') as weekday,
    to_char(next_day(mydate,'sunday'),'iw') as week_num
    FROM ( SELECT TRUNC (SYSDATE, 'yy') - 1 + LEVEL AS mydate
    FROM dual
    CONNECT BY LEVEL <= (SELECT TRUNC (ADD_MONTHS (SYSDATE, 24), 'yy')
    - TRUNC (SYSDATE, 'yy')
    FROM DUAL))
    this query gives date, weekday and week_num for 2 years 2013 and 2014,
    when i run this query ,at the end of the 2013 it gives the result as,
    26-DEC-13     thursday      52
    27-DEC-13     friday      52
    28-DEC-13     saturday      52
    29-DEC-13     sunday      01
    30-DEC-13     monday      01
    31-DEC-13     tuesday      01
    01-JAN-14     wednesday     01
    02-JAN-14     thursday      01
    for dates 29 ,30,31st it should give 53 .. how can i achieve that using this this query .. can any one help me out on this please...
    thanks,
    pradeep

    I tried with the IW ...
    it is giving week_id for the year.
    select
    mydate,
    to_char(mydate,'day'),
    case when mydate between trunc(mydate,'yyyy') and next_day(trunc(mydate,'yyyy'),'saturday')
    then to_number(to_char(mydate,'yyyy')||to_char(trunc(mydate,'yyyy'),'iw'))
    when mydate between next_day(trunc(mydate,'yyyy'),'saturday') and trunc(add_months(trunc(mydate,'yyyy'),12)-1,'d')-1
    then to_number(to_char(mydate,'yyyy')||to_char(next_day(mydate,'sunday'),'iw'))
    when mydate between trunc(add_months(trunc(mydate,'yyyy'),12)-1,'d') and add_months(trunc(mydate,'yyyy'),12)-1
    then to_number(to_char(mydate,'yyyy')||to_char(trunc(add_months(trunc(mydate,'yyyy'),12)-1,'d')-1,'iw')+1) end as WEEK_ID
    FROM ( SELECT TRUNC (SYSDATE, 'yy') - 1 + LEVEL AS mydate
    FROM dual
    CONNECT BY LEVEL <= (SELECT TRUNC (ADD_MONTHS (SYSDATE, 24), 'yy')
    - TRUNC (SYSDATE, 'yy')
    FROM DUAL
    ))

  • XSLT Mapping: how to calculate Week number of the year from given date

    Hi,
    I  have input as date, i need to know the Week number from that date in XSLT Coding.
    for Eg: if date is 29-12-2009 it should give 53rd week of the year.
    All answers will be appreciated.
    Regards,
    Mayank

    Hi add this statement to your XSLT Mappping
    <xsl:stylesheet version="1.0" xmlns:java="http://xml.apache.org/xslt/java" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" exclude-result-prefixes="java">
    and use <xsl:template name="GetDateInLocal" xmlns:cal="xalan://java.util.GregorianCalendar"> in XSLT Mapping , write your logic.
    cheers,
    Raj

  • How to get a proper week number and MATCHING year number

    Java classes such as SimpleDateFormat offer convenient means to find out the correct week number of a given date. Week numbering is locale specific and not always intuitive. For example, Dec 31, 2001 is in week 1 of 2002 in locales where a week starts with Monday or Sunday. Now, if one formats the date using a method such as:
    new SimpleDateFormat("'week: 'ww', year: 'yyyy").format(theDate);
    this produces result
    week: 1, year: 2001
    when the typically anticipated value would be "week: 1, year: 2002".
    Is there any means to find out the year number, which matches to the given week number for a date? Or do I have to create a kludge myself?
    I would be suprised if this feature is not part of Java API, since it seems like a quite common need. Then again, I'm not suprised by surprises in Java API anymore ;)

    Well, the real problem is just that the rules determining which year the week belongs to are complicated. In some locales, weeks start with Monday, in some Sunday. In some locales year that has 4 or more days of the week "owns" the week, in some locales there might be different rules.
    All this complex logic is already implemented in Calendar / SimpleDateFormat / etc classes, so it would be clearly valuable thing to be able to use that code. For example, Calendar.get(YEAR_OF_WEEK_OF_YEAR) or similar. But, it does not seem to be possible.
    As I mentioned originally, I can implement my own "kludge" to do this if I have to. This is not nice, since I will be duplicating the functionality already implemented in the calendar classes. Of course, solution is neither as simple as "set calendar to the weeks end" or "set calendar to the weeks start"...

  • Week Number wrt Fisacal year

    i want week no with respect to  Fisical year (Apr2009-Mar2010) for Input date? Fisical year (Apr2009-Mar2010)  keep changes from record to record. is there any standard function module or should i do any coding.
    thanks
    Raghu

    Hi Raghu,
    I am sorry I dint notice that you are not using a fiscal year variant. Fiscal year variants are specifically given for such puproses when you want to define your own year begins and ends unlike the calendar year.
    Pls read thru this.
    http://help.sap.com/saphelp_40b/helpdata/fr/95/e42c7dd435d1118b3f0060b03ca329/content.htm
    I am sure this will be helpful.
    Thanks
    Mani

  • Parameter for Last Week, Last Month & Last Year from the current Date

    I have a crystal report that has a StartDate & EndDate parameters from the stored procedure that I am calling. Now i got a new requirement saying that my user wants a drop-down parameter in which they need to be able to select last week (Sunday to Saturday), last month, last year instead of manually selecting start date and end date parameters? Can anyone please explain me how to do this?
    (FYI, I am using Crystal 2008)

    Hi Naveen,
    You'll need to handle the logic in the Stored Proc's where clause.
    If you're using oracle, I have something like this in the Stored Proc's where clause for our reports:
    Where {Date_Field} Between
    Decode('{?Relative Date}',
    'Yesterday', sysdate - 1,
    'Last Week', Tunc(sysdate, 'IW')-7)
    AND
    Decode('{?Relative Date}',
    'Yesterday', sysdate,
    'Last Week', Tunc(sysdate, 'IW'))
    where 'Relative Date' is a prompt in the Stored Proc and 'yesterday'. 'last week' are the 'static values' in the prompt.
    -Abhilash

  • How to get the week number of year from a Date object?

    Hi!
    I would like to know the week number of the year from a specified Date.
    I dont know how to set the first day of week, and set the minimum days of a week.
    I want to use Monday for first day of week, and at least 4 days of month in a week.
    For example if its 1st, January is Friday, then the first week starts on 4th, Monday.
    Anyone can help?

    Sorry. I should think before I move.
         public static int getWeekOfYear(Date date) {
             Calendar calendar = Calendar.getInstance();
             calendar.setTime(date);
             calendar.setFirstDayOfWeek(Calendar.MONDAY);
             calendar.setMinimalDaysInFirstWeek(4);
             return calendar.get(Calendar.WEEK_OF_YEAR);
         }

  • IPCC 7 Determine the week number of a month

    Hello,
    I am working towards determine the week number of month and make appropriate routing changes only for that specific week. I have tried using the trunc(), getdate() and weekday(). But i fail to get the week number of a month, but I believe I have managed to the week number of a year.
    The example I have considered is: On Tuesday and Thursdays in second week of every month the calls to be routed to a different skillgroup.
    Please let me know if anyone has any suggestion for this.
    Thanks,
    FYI,
    CM 4.1, CRS 3.5, ICM 7.1

    Hi buddy,
    Great stuff!! Your link gave me another hint of the possible solution, I was working on ICM but not sure why i did not consider CRS for this requirement , The CRS has a builtin function, I have used this builtin Methods which provided me what i expected.
    The debug on the script resulted the week of the month for today which is "4".
    Please let me know your view.
    Thanks again!

  • How to determine the month from the week number?

    Hi all,
    in a routine i have the week number in a year and i need to find the month but i don't know the correct FM to use for this.
    Is there anyone that had to face a similar problem?
    I'm considering that to determine the month it will be used the first day of the week since there are weeks belonging to 2 different months...
    Thank you
    Stefano

    Hi
    If you have a Fiscal Year Variant determined for the week you can use function module PERIOD_DAY_DETERMINE by passing week,fiscal year and fiscal year variant for the week which will return fist and last date.So based on last data you can determine the month.
    Regards
    Srilaxmi

  • Display ISO week number instead of date on x axis in Bar Chart

    Hi,
    I've created a simple SSRS report based on bar chart that shows several milestones. Everything works fine for me except I’m not able to convert the date into ISO week number format.
    I played around with different approaches. I was able to convert the date into an ISO week Format directly on the SQL Server. That
    wasn't a problem.
    But unfortunately I’m not able to display the week number on the horizontal axis in my Bar Chart. I tried both fields: TaskFinishDate and TaskFinishDateMS...
    I would like to show the ISO week number instead of the date within the Bar Chart on the horizontal axis.
    Any ideas/hints/help is really appreciated!
    Thanks,
    Mike

    Hi Mike,
    Per my understanding that you want to get the week number of the year based on the field "TaskFinishDate" which is datetime type and display the week number in the x-axis instead of the field "TaskFinishDate", right?
    I have check the snapshot you have provided and it seems you have change the format of the datetime field in the x-axis like "dd.MM.YYYY", If you can't make the week number to display correctly in the x-axis, the issue can be caused by you haven't
    change the format to Number in the category.
    Details information below for you reference:
    I assume you have use expression in the Label like below to convert the datatime TaskFinishDate in to ISO week number like below:
    =DatePart(DateInterval.WeekOfYear,Fields!TaskFinishDate.Value)
    or
    =DatePart("ww",Fields!TaskFinishDate.Value)
    Right click the X-axis and select the "Horizontal Axis Properties", then click the Number to change the format to "Number" as below:
    Preview you will get the weeknumber display in the x-axis correctly.
    If you still have any problem, please feel to ask.
    Regards,
    Vicky Liu
    If you have any feedback on our support, please click
    here.
    Vicky Liu
    TechNet Community Support

  • FM for week number of month

    Hi all,
       Could you suggest me a FM for for finding the week number of a month.
       I can see FM which shows week number of an year .But i want the week number with reference to a month.
    Thanks
    Sam.

    Hi,
    A sample using function DATE_GET_WEEK 
    Report try05.
    DATA: xweek(6)    TYPE n,
                        xdt      TYPE d.
    xdt = '20050101'.
    CALL FUNCTION 'DATE_GET_WEEK'
      EXPORTING
        DATE               = xdt
    IMPORTING
       WEEK               = xweek
    EXCEPTIONS
       DATE_INVALID       = 1
      OTHERS             = 2
    IF SY-SUBRC <> 0.
    MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
            WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
    ENDIF.
    WRITE:/ xdt.
    WRITE:/ xweek.
    Hope this helps.
    Reward if helpful.
    Regards,
    Sipra

  • How do i get the date when enter a week number

    Hi, can anyone tell me how do i get the date when enter a week number??
    let say, i enter week number 4 and year 2008
    result=26-01-2008
    thanks..

    import java.util.Calendar;
    public class testCalendar {
         public static void main(String args[]){              
              int year=2008;
              int week =5;
              Calendar now=Calendar.getInstance();
              now.set(Calendar.YEAR,year);
    now.set(Calendar.WEEK_OF_YEAR,week);
              System.out.println(now.getTime());          
    hai, anyone know why i can't get the result:27-01-2008
    i always get two day later. That is 29-01-2008
    no wonder how i change the week number and year.
    for example: week=7, year=2007
    expected result:11-02-2007
    but the result is 13-02-2007
    thanks..
    Edited by: Angel_Wei on Sep 1, 2008 11:58 PM

  • Week number calculations not working ...

    Why doesn the week number calculation (ISO 8601) using datepart ('ww',#date#,crMonday,crFirstFourDays) not work?
    As an example do the following formula:
    DatePart('ww',CDate(2005,1,1),crMonday,crFirstFourDays)
    The result is 9363, quite an impressive week number
    This is an issue in versions 2008, XI R2, XI, 10 and possibly previous versions too

    Hi Raghavendra
    No I want to be able to calculate week number according to the week numbering definition from ISO 8601 - have a look at [http://en.wikipedia.org/wiki/ISO_week_date|http://en.wikipedia.org/wiki/ISO_week_date]
    If you want to use datepart ('ww, ... to solve this,
    you have to specify crMonday as third argument and crFirstFourDays as argument number 4 to tell datepart that weeks starts on mondays and week #1 is the one with the first 4 days of the year.
    Doing this makes my example date  used with datepart returns the week number 9363!!
    The DatePart('ww',CDate(2005,1,1),crMonday,crFirstFourDays) is supposed to return 53!!
    Weeknumbers are supposed to be between 1 and 52 or 53 depending of the year.
    Be aware weeks number 1, 52 and 53 very often contains days from 2 consequtive years, when defined according to ISO 8691.
    Your suggestion does not take into account the fact Weeks start on mondays and january 1st is not always the first day of week 1.
    It returns week number 1
    In fact it seems every day in the start of a year with weeknumber starting in the previous year gets the weeknumber calculated wrong.
    Regards,
    Poul
    Do you want to calculate the weeknumber of a particular date? If so please try datepart('ww',CDate(2005,1,1)).
    If not could you please explain me what you are trying to calculate and what output you are expecting?
    Regards,
    Raghavendra

  • How to display week number in a report

    Hi,
    I need to show the week number in a report and related sales data for that week. For example, starting from 01-JUL-2009 the week1 should show total of sales from 01-JUL-08-JUL.
    Help please....
    Thanks
    A/A

    If you are trying to extract the week number of the calender year...you can use..
    sql> select to_char(sysdate,'WW') from dual;
    TO
    05and then group the sales figures by week...
    sql> create table t(
      2     target_date date,
      3     sales       number);
    Table created.
    sql> begin
      2     for i in 1..30 loop
      3        insert into t values (sysdate-i,100*i);
      4     end loop;
      5   commit;
      6  end;
      7  /
    PL/SQL procedure successfully completed.
    sql> select week, sum(sales) from (
      2     select sales, to_char(target_date,'WW') week from t)
      3   group by week;
    WE SUM(SALES)
    05       2100
    02      16800
    01       8800
    04       7000
    03      11900However, if your week calculation starts with a particular date like a calender year starting with 01-june-2010, then you would need to calculate the (relative) week number using something like...
      1  select target_date,
      2         trunc(((target_date - to_date('01-june-2010','DD-MON-YYYY'))/7)) week_number
      3*   from t
    sql> /In the above code, 01-june-2010 is the date from which my calender year starts.
    Please note that since this is relative, you might get negative numbers for weeks.

  • Get week number for day from a list of days within a year..

    I am beginning in ORACLE / SQL and wondering how I can use this query to work with a column of Dates that span over a number of years?
    Ideally, I would like to use this script to add a number for the week of the year based on this.
    Then when a new year begins to start over..
    I am referencing an old thread..
    TO_CHAR with dates to get week number issue
    Here is what I have so far:
    I keep getting an error stating "Bind Variable "p_date_beg" is NOT DECLARED"..
    Thanks in advance for any help you can provide.
    DECLARE
    p_date_beg DATE;
    BEGIN
    SELECT RETRIEVAL_DATE
    INTO P_DATE_BEG
    FROM OBS_SELECT_LST12_SPG;
    WITH TAB AS
    (SELECT TO_DATE(TO_CHAR(:p_date_beg,'YYYYMM')||'01','YYYYMMDD') + LEVEL -1 DATE_COL
    FROM DUAL
    CONNECT BY LEVEL <=TO_NUMBER(TO_CHAR(LAST_DAY(:p_date_beg),'DD')))
    SELECT DATE_COL, TO_CHAR(DATE_COL, 'FMDAY') DAY,
    (CASE
    WHEN TO_CHAR(TRUNC(DATE_COL, 'mm'),'FMDAY') = 'SUNDAY'
    THEN TO_NUMBER(TO_CHAR(DATE_COL,'W'))
    ELSE CEIL((TO_CHAR(SYSDATE, 'dd') + 1 - TO_CHAR(NEXT_DAY(TRUNC(SYSDATE, 'mm'), 'SUNDAY'), 'dd'))/7)
    END) WEEK_NOB
    END;

    hi, referring to this link about ISO week, http://www.epochconverter.com/date-and-time/weeknumbers-by-year.php, here is the code:
    /* Formatted on 2012/06/13 06:32 (Formatter Plus v4.8.8) */
    WITH t AS
         (SELECT     MIN (TO_DATE ('20120101', 'rrrrmmdd') + LEVEL - 1)
                                                                       start_date,
                     1 week_th
                FROM DUAL
               WHERE TO_CHAR (  TRUNC (TO_DATE ('20120101', 'rrrrmmdd'), 'mm')
                              + LEVEL
                              - 1,
                              'FMDAY'
                             ) = 'MONDAY'
                 AND (   TO_DATE ('20120101', 'rrrrmmdd') + LEVEL - 1 <
                            TRUNC (NEXT_DAY (TO_DATE ('20120101', 'rrrrmmdd'),
                                             'THU'
                      OR (TO_DATE ('20120101', 'rrrrmmdd') + LEVEL - 1
                             BETWEEN TRUNC (NEXT_DAY (TO_DATE ('20120101',
                                                               'rrrrmmdd'
                                                      'THU'
                                 AND   TRUNC (NEXT_DAY (TO_DATE ('20120101',
                                                                 'rrrrmmdd'
                                                        'THU'
                                     + 7
          CONNECT BY LEVEL < 15),
         v AS
         (SELECT     t.start_date + 7 * (LEVEL - 1) start_date,
                     t.week_th + LEVEL - 1 week_th
                FROM t
          CONNECT BY LEVEL < 53),
         u AS
         (SELECT '20120501' dt
            FROM DUAL
          UNION ALL
          SELECT '20120502'
            FROM DUAL
          UNION ALL
          SELECT '20120503'
            FROM DUAL
          UNION ALL
          SELECT '20120504'
            FROM DUAL
          UNION ALL
          SELECT '20120505'
            FROM DUAL
          UNION ALL
          SELECT '20120506'
            FROM DUAL
          UNION ALL
          SELECT '20120507'
            FROM DUAL
          UNION ALL
          SELECT '20120508'
            FROM DUAL
          UNION ALL
          SELECT '20120509'
            FROM DUAL)
    SELECT *
      FROM u, v
    WHERE TO_DATE (u.dt, 'rrrrmmdd') BETWEEN v.start_date AND (v.start_date + 6);output:
    DT     START_DATE     WEEK_TH
    20120501     30/04/2012     18
    20120502     30/04/2012     18
    20120503     30/04/2012     18
    20120504     30/04/2012     18
    20120505     30/04/2012     18
    20120506     30/04/2012     18
    20120507     07/05/2012     19
    20120508     07/05/2012     19
    20120509     07/05/2012     19

Maybe you are looking for

  • Can I exchange my water damaged 5s for a reconditioned phone or have it fixed?

    My young son decided that my new (3month old) 5s iphone needed to go into a full sink of water with the breakfast dishes....I have no idea how long it was in the sink, but I have tried to dry it out over a few days and when I finally plugged in the c

  • IO Error while creating the Datasource in WLS Console

    Hello, We are using Jdeveloper 11.1.1.1.7, I am trying to create a Data Source using WLS Console, I am getting below error   - I tried creating DB connection using SQL developer with same DB Connection info, it works fine.   - I verified the Port by

  • Need to reinstall Photoshop on new computer

    Stolen laptop...got new one and need to reinstall Photoshop CS3 on my new one. Adobe "customer service" is not helpful. I have the original $1,500 software, but when I try to install it. I get an error notice and it only installs a portion (PDF). Adv

  • How to Use Cutaway Feature in iMovie '11

    Hi. I am attempting to make a farewell movie for a retiring colleague. I have video and digital photos, but here is the clincher: My video is of people talking and it is boring to watch. Therefore, I want to run video and photo "slides" in the same p

  • Nokia Stereo Headset HS-45, AD-43 Problems need h...

    HI After purchasing my N95 I noticed that my Nokia Stereo Headset HS-45, AD-43 for some reason don't work. I can hear sound but all buttons wont work . Can someone tell me how to check is this think working or not. Because I just get burnd from http: