Week of the year (from Monday to Sunday)

Hi guys,.

I searched 'Week of the year' and that you have not found a solution to my problem.

I think that the standard Oracle 'IW' function returns the week of the year beginning January 1.  This year, for example, Monday, January 6 is back as week 1 because on 1 January was Wednesday, therefore, it is in the 1st 7 days of the year.

Select to_char (to_date(:p_date,'DD/MM/YYYY'), 'IW') wrong_week_no

, to_char (to_date (: p_date + 2, ' DD/MM/YYYY'), 'IW') correct_week_no

of the double

I would like the query to return the week number from Monday to Sunday, so in this case, the 6th should return as number 2 of the week.

In the query above, by adding 2 to the date gives me the correct week number but won't work for 2015 and 2016.

Any help would be appreciated.

I use the Oracle 10 g platform.

Thank you

Chris

Do you mean like this?

SQL > break the month jump 1
SQL > set linesize 200
SQL > set pagesize 2000
SQL > column month format a20
SQL > column week format a4
SQL > with req as (select '& Required_Year_YYYY' year of double)
2, offset as (select case when to_char (trunc (to_date (yr, 'YYYY'), 'YYYY'), 'IW') in ('52 ', ' 53') then 1 else 0 end as the offset of req)
3 select lpad (Month, - 20 (20-length (month)) / 2) months.
4 '('|| semaine ||') ' as the week, "Mo", "You", "We", "Th", "Fr", "Sa", "knew".
5 of)
6. Select to_char (dt, 'fmMonth YYYY') month,
7 case when to_char (dt, 'mm') = '12 ' and to_char (dt, 'iw') = ' 01' and offset = 0 then ' 53 "
8 when to_char (dt, 'mm') = '12 ' and to_char (dt, 'iw') = '01' and offset = 1 then ' 54'
9 when to_char (dt, 'mm') = '01' and to_char (dt, 'iw') and ('52', ' 53 ") then"1"
10 to_char (to_number (to_char (dt, 'iw')) + offset) other end than the week,.
11 max (decode (to_char(dt,'d'), '1', lpad (to_char (dt, 'GFMD') 2)),) "Mo."
12 max (decode (to_char(dt,'d'),'2 ', lpad (to_char (dt, 'GFMD'), 2))) "you."
Max (decode (to_char(dt,'d'),'3 ', lpad (to_char (dt, 'GFMD'), 2))) 13 '' We. ''
14 max (decode (to_char(dt,'d'),'4 ', lpad (to_char (dt, 'GFMD'), 2))) "Th."
15 max (decode (to_char(dt,'d'),'5 ', lpad (to_char (dt, 'GFMD'), 2))) "Fr."
16 max (decode (to_char(dt,'d'),'6 ', lpad (to_char (dt, 'GFMD'), 2))) "his."
17 max (decode (to_char(dt,'d'),'7 ', lpad (to_char (dt, 'GFMD'), 2))) "Su".
18 in (select trunc (to_date (req.yr, 'YYYY'), 'y')-1 + rownum dt)
19 object, req
20 where rownum<= add_months(trunc(to_date(req.yr,'yyyy'),'y'),12)="" -="" trunc(to_date(req.yr,'yyyy'),'y')="">
21, offset
22 group by to_char (dt, 'fmMonth AAAA'), case when to_char (dt, 'mm') = '12 ' and to_char (dt, 'iw') = ' 01' and offset = 0 then ' 53 "
23 when to_char (dt, 'mm') = '12 ' and to_char (dt, 'iw') = '01' and offset = 1 then ' 54'
24 when to_char (dt, 'mm') = '01' and to_char (dt, 'iw') and ('52', ' 53 ") then"1"
25 to_char (to_number (to_char (dt, 'iw')) + offset) to another end
(26) x
27 order by to_date (month, "Month YYYY"), to_number (x.week)
28.
Enter the value for required_year_yyyy: 2012
old 1: with req as (select '& Required_Year_YYYY' year of double)
new 1: with req (select '2012' year of double)

MONTH WEEK Mo Tu we Th his di ve
-------------------- ---- -- -- -- -- -- -- --
January 2012 (1) 1
(2) 2 3 4 5 6 7 8.
(3) 9 10 11 12 13 14 15.


(4) 16 17 18 19 20 21 22.
(5) 23 24 25 26 27 28 29.
(6) 30 31.

February 2012 (6) 1 2 3 4 5
(7) 6 7 8 9 10 11 12.
(8) 13 14 15 16 17 18 19.
(9) 20 21 22 23 24 25 26.
(10) 27 28 29.

March 2012 (10) 1 2 3 4
(11) 5 6 7 8 9 10 11.
(12) 12 13 14 15 16 17 18.
(13) 19 20 21 22 23 24 25.
(14) 26 27 28 29 30 31.

April 2012 (14) 1
(15) 2 3 4 5 6 7 8.
(16) 9 10 11 12 13 14 15.
(17) 16 17 18 19 20 21 22.
(18) 23 24 25 26 27 28 29.
(19) 30

May 2012 (19) 1 2 3 4 5 6
(20) 7 8 9 10 11 12 13.
(21) 14 15 16 17 18 19 20.
(22) 21 22 23 24 25 26 27.
(23) 28 29 30 31.

June 2012 (23) 1 2 3
(24) 4 5 6 7 8 9 10.
(25) 11 12 13 14 15 16 17.
(26) 18 19 20 21 22 23 24.
(27) 25 26 27 28 29 30.

July 2012 (27) 1
(28) 2 3 4 5 6 7 8.
(29) 9 10 11 12 13 14 15.
(30) 16 17 18 19 20 21 22.
(31) 23 24 25 26 27 28 29.
(32) 30 31.

August 2012 (32) 1 2 3 4 5
(33) 6 7 8 9 10 11 12.
(34) 13 14 15 16 17 18 19.
(35) 20 21 22 23 24 25 26.
(36) 27 28 29 30 31.

September 2012 (36) 1 2
(37) 3 4 5 6 7 8 9.
(38) 10 11 12 13 14 15 16.
(39) 17 18 19 20 21 22 23.
(40) 24 25 26 27 28 29 30.

October 2012 (41) 1 2 3 4 5 6 7
(42) 8 9 10 11 12 13 14.
(43) 15 16 17 18 19 20 21.
(44) 22 23 24 25 26 27 28.
(45) 29 30 31.

November 2012 (45) 1 2 3 4
(46) 5 6 7 8 9 10 11.
(47) 12 13 14 15 16 17 18.
(48) 19 20 21 22 23 24 25.
(49) 26 27 28 29 30.

December 2012 (49) 1 2
(50) 3 4 5 6 7 8 9.
(51) 10 11 12 13 14 15 16.
(52) 17 18 19 20 21 22 23.
(53) 24 25 26 27 28 29 30.
(54) 31

64 selected lines

I used 2012 for example, because in this case, it has 54 weeks.

So, here we have 1 January during the week 1, but 2 January's second week, as is Monday, etc.

Tags: Database

Similar Questions

  • How to get the week of the year in American Standard

    Hello

    I know that it might be a repetitive question, but has not found a convincing solution to him in any of the previous threads. I'm looking for a function (user defined, if someone has already written it) go the week of the year, American Standard. i.e., The week should begin on Sunday and end on Saturday (and NOT from Monday to Sunday)

    03/01/2010 - should be the beginning of the 1st week of 2010... 10/01/2010 will be the beginning of the second week and so on
    04/01/2009 - should be the beginning of the 1st week of 2009... 11/01/2009 will be the beginning of the second week and so on.

    Does anyone have a function that takes a date as input and returns back the week of the year in the above format? Any help is greatly appreciated.

    Thank you

    You can add a day to your date in order to manipulate the weeks;

    with t as (
       select to_date('02/01/2010','dd/mm/yyyy') dt from dual union all
       select to_date('03/01/2010','dd/mm/yyyy') from dual union all
       select to_date('09/01/2010','dd/mm/yyyy') from dual union all
       select to_date('10/01/2010','dd/mm/yyyy') from dual)
    select dt, to_char(dt, 'Day') dy, to_char(dt+1, 'iw') wk
    from t;
    
    DT          DY        WK
    ----------- --------- --
    02/01/2010  Saturday  53
    03/01/2010  Sunday    01
    09/01/2010  Saturday  01
    10/01/2010  Sunday    02
    
  • How to group the values according to the weeks of the year?

    Hello

    I have a table with dates and values (I'm simplifying the need for this question)

    I want to group the values (and adding the) according to the 52 weeks of the year. I mean week of consolidation for the week.
    Here the date of each weeks generated during a year, it gives every Monday of each week of the year:
    Select trunc (trunc (sysdate, 'year'), 'iw') +(rownum*7)-7 n of object where rownum < = 53

    What I want to do is to summarize the values in my table and group together them in every week, in that respect. The date of these weeks are generated by the above query.

    For example all values fall into the 1st week of the year will be grouped into this date, all values fall in the 2nd wek will be grouped in this date and so on. If no value is found for a week, then a value of 0 is generated in iorder have a 52 weeks-lines. It's basically a kind of select with "date between... and...". "and group values and that affect the number of the wee, they are relevant.

    Examples of Monday, June 30, 2008, 07/07/2008, 07/14/2008

    Example of values and dates in my table:

    Date value
    30/06/08 9
    07/01/08 9
    07/02/08 9
    07/03/08 9
    07/03/08 9
    07/04/08 6
    07/04/08 6
    07/04/08 9
    07/07/08 9
    07/08/08 9
    29/07/08 9
    29/07/08 6
    30/07/08 9
    07/31/08 9
    07/31/08 9
    07/31/08 9
    08/01/08 9
    08/04/08 5.5
    08/04/08 9
    08/07/08 9
    08/07/08 9
    08/08/08 9
    14/08/08 9
    14/08/08 9
    18/08/08 9
    22/08/08 9
    22/08/08 9
    25/08/08 9
    25/08/08 9
    26/08/08 9
    29/08/08 9
    29/08/08 9
    09/01/08 9
    09/01/08 9
    09/04/08 9
    09/04/08 9
    09/05/08 9
    09/05/08 9
    09/05/08 9
    09/05/08 9
    17/09/08 9

    Result should be something like this:

    Number of the week Date value
    47/1 12/31/2008
    94/2 07/12/2008
    2008-01-14 3, 23
    and so on...
    .....
    73/52 12/22/2008
    60/53 29/12/2008

    I hope I am clear. Thank you for your kind replies.

    Something like that, maybe:

    select trunc(trunc(sysdate,'year'),'iw') +((level-1)*7)  n
    from dual connect by level<=53
    

    (I changed it to sysdate so that the next years will take care of them!)

  • How to get the previous week from Monday to Sunday from sysdate

    for example, sysdate is 15-SEP-07 We must retrieve the data from SEPT. 01.-15-06 - SEP - 2015, although 31 August 15 is Monday our application must ignore since the current month is September

    SQL> with t
      2  as
      3  (
      4  select to_date('07-SEP-15', 'dd-mon-rr') my_sysdate
      5    from dual
      6  )
      7  select greatest(trunc(my_sysdate, 'iw') - 7, trunc(my_sysdate, 'month')) start_date
      8       , trunc(my_sysdate, 'iw') - 1 end_date
      9    from t;
    
    START_DAT END_DATE
    --------- ---------
    01-SEP-15 06-SEP-15
    
  • Store the value less than the current week and the year.

    Hi all

    I have a table called BACK, which structure is like this
    ITEM_NUMBER   YEAR_WEEK   MS
    ...........   .........   ..
    1N58          2012-WK02   01/15/2012
    1N58          2011-WK02   01/15/2011
    current my procedure I store the value in my new table that is less than the current week. But I want to retain the value less than the current week and the current year. Please, help me to get how I can do this.
    PROCEDURE BACKLOG_PROC_LT_CW IS
      BEGIN
     DELETE BACKLOG_LT_CW;
     COMMIT;
          INSERT INTO BACKLOG_LT_CW
            SELECT 
                  DC_UTIL.GEN_YEAR_WEEK(MS) YEAR_WEEK,
                  ITEM_NUMBER,
                  MSD
    
                  FROM BACKLOG
              where to_char(MS, 'IW')<to_char(sysdate, 'IW');
    
      END BACKLOG_PROC_LT_CW;
    Currently, with the above code is to store the value in my table of BACKLOG_LT_CW but if you see the data in the table from the BACK you will find YEAR_WEEk 2012-WK02 falls next year, but again it is store in my table because IW took only. Please help me get the soln

    Thanks in advance

    Concerning

    Are you looking for this?

    ...
    where to_char(MS, 'YYYYIW')
    
  • first day of the year and Monday next year

    Hello all;

    Given a year, how to determine the first day of this year and the next two Mondays for this year...

    This gives a shot:

    SELECT TRUNC(TO_DATE('2010','YYYY'),'YEAR')                        AS first_day_of_year
         , NEXT_DAY(TRUNC(TO_DATE('2010','YYYY'),'YEAR')-1,'MONDAY')   AS first_monday_of_year
         , NEXT_DAY(TRUNC(TO_DATE('2010','YYYY'),'YEAR')-1,'MONDAY')+7 AS second_monday_of_year
    FROM   dual
    ;
    
  • Get dates for the Monday of the week of the year for 3 years

    I try to get the dates for on Monday for the last 3 years.

    I am currently using a query that does not provide the correct results. I tried to format it with IW and WW for the week.

    Y at - it an easy way to get the list of dates every Monday beginning in 2007.

    Best regards
    Sumit.
    select  trunc(date '2006-12-31','iw') + 7 * level monday
      from  dual
      connect by trunc(date '2006-12-31','iw') + 7 * level <= sysdate
      order by level
    /
    
    MONDAY
    ---------
    01-JAN-07
    08-JAN-07
    15-JAN-07
    22-JAN-07
    29-JAN-07
    05-FEB-07
    12-FEB-07
    19-FEB-07
    26-FEB-07
    05-MAR-07
    12-MAR-07
    
    MONDAY
    ---------
    19-MAR-07
    26-MAR-07
    02-APR-07
    09-APR-07
    16-APR-07
    23-APR-07
    30-APR-07
    07-MAY-07
    14-MAY-07
    21-MAY-07
    28-MAY-07
    
    MONDAY
    ---------
    04-JUN-07
    11-JUN-07
    18-JUN-07
    25-JUN-07
    .
    .
    .
    

    SY.

  • extract the week of the year

    Hello

    How to extract of the week from the date in the obiee11g functions


    Thank you

    Syntax: WEEK_OF_YEAR ("calendar". "Date") "

    for week:WEEK_OF_YEAR (current_date) courses

    Kind regards
    Disney

  • Deriving from the first day of the week ww in the year yyyy

    I need to be able to display the first day of a particular week in addition to the "week of the year" and the year. for example:
    select to_char("ServiceDate",'yyyy-ww') as "Year/Week", sum("Points") as "Points"
    from my_table
    group by to_char("ServiceDate",'yyyy-ww')
    order by to_char("ServiceDate",'yyyy-ww')
    give me the following:
    Year/Week     Points
    2008-33            1                      
    2008-35            1                      
    2008-42            4                      
    2008-43            2                      
    2009-01            1                      
    ...
    and so on. What I would like to add to the dataset is the same day, the first day of the week for this week/year. For example, the first line would look like this:
    Year/Week     1st day of week     Points
    2008-33          08-AUG-2008*         1
    * Or whatever the first day of the week 33 of 2008 is (assuming that "first day of the week" is a Sunday, in my case).

    My brain is on "dim" right now; any help is greatly appreciated.
    Thanks in advance,
    Carl
    select  to_char(trunc("ServiceDate",'ww'),'yyyy-ww') as "Year/Week",
            to_char(trunc("ServiceDate",'ww'),'DD-MON-YYYY') "Week Start Date",
            sum("Points") as "Points"
    from my_table
    group by trunc("ServiceDate",'ww')
    order by trunc("ServiceDate",'ww')
    /
    

    SY.

  • HP: I need the month and the year of manufacture of the following numbers. Can you please help

    Can you please help me with the manufacturing of the year and the month of the following computers portable

    [Personal information]

    Hello:

    Take a look at the first serial number you posted...

    The 4th character from the left is the year of manufacture.

    In the first case, it is either 2005, 2015 according to the model (that would be obvious based on the hardware of the laptop has, or when you bought).

    The characters of 5th and 6th from the left indicate the week of manufacture.

    If you need to take another look at the first serial number because 3-b is probably a 38 (which means the 38th week of the year).  Like all the others who have two numbers in the 5th and 6th places.

    The 38th week of the year is sometime in September.

  • By selecting dates from Friday to Sunday?

    Hello people,
    Try to work on a request where there is a selection of date in the where clause. That is, if sysdate is Monday, I get the data from Friday to Sunday.
       and sh.change_date >=
                   to_date('20110304 000000', 'YYYYMMDD HH24MISS')
               and sh.change_date <=
                   to_date('20110306 235959', 'YYYYMMDD HH24MISS')
    Am now hard-coding the dates I want to see in the change_date. But is it possible that every time when I run the query didn't change these date values?

    Thanks a bunch!

    Hello

    Thank you. Now that I can test the code I posted, I see that there is a typing error in TO_CHAR arguments.
    That's what you want:

    DEFINE  test_sysdate = "DATE '2011-03-08'"
    
    SELECT     *
    FROM     test_a
    WHERE   change_date    >= TRUNC (&test_sysdate) - CASE  TO_CHAR ( &test_sysdate
                                                             , 'Dy'
                                              , 'NLS_DATE_LANGUAGE=ENGLISH'     -- If necessary
                                              )
                                        WHEN  'Mon'  THEN  3
    --                                    WHEN  'Sun'  THEN  2
                                                         ELSE  1
                                                END
    AND     change_date < TRUNC (&test_sysdate)
    ;
    

    except that when I used the proxy & test_sysdate above (to make it easy test), you use SYSDATE.

    I have commneted on the particular case of when you run the query on Sunday. You probably want is a comment this line or remove it completely.

  • To get the last date of the year

    Can someone tell me how to get the date of the year from the first date of last year:
    My query below gives the last date of the month

    SELECT LAST_DAY (ADD_MONTHS (TO_DATE (JANUARY 1, 2012 "," DD/MM/YYYY '), 12-))
    TO_NUMBER (to_char (sysdate, 'mm'))) OF the DOUBLE

    Thnx in advance
    SELECT LAST_DAY (ADD_MONTHS (TO_DATE ('01/01/2012', 'DD/MM/YYYY'), 11))
      FROM DUAL
    
  • extracting remaning months of the year during the current month

    Hello

    I like allows you to retrieve the other months of the year from the current date.
    Can I do this?

    For example, it is today, July 23, 2012.
    Then, I would like to have:

    August
    September
    October
    November
    December

    Thank you
    Diana

    Hi, Diana,.

    Here's one way:

    SELECT     TO_CHAR ( ADD_MONTHS (SYSDATE, LEVEL)
              , 'Month'
              )    AS month_name
    FROM     dual
    CONNECT BY     LEVEL     <= 12 - EXTRACT (MONTH FROM SYSDATE)
    ;
    

    What output would you want in December?

  • Gets harassed by email from someone every week about the record of the arrest.

    original title; email address: * email address is removed from the privacy * is harassing me

    Hello

    I was hassled by email of this person each week about the record of the arrest.  See below.  Is there a way to discover who is behind this address loooong- [email protected]?  I think I know who she is, but not sure at 100%.  I want to file charges of harassment on my own, but I need to know if it's really him before anything else.  Thanks, M.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    Date: Monday, October 1, 2012 05:41:05-0700
    From: [email protected]
    Subject: Your record of arrest
    TO:

    CAVEAT!
    Your record of arrest may be available to the general public!

    You made it obvious. Some people do not read before they jump in with an answer.

  • Function to create dates as well as the week, quarter, and year

    Hi all

    I am trying to write a function that will create automatically with their respective week, quarter, and year dates in different columns.

    For example,.

    Date of the quarter Qtr/Yr/QtrWk week exercise exercise exercise

    415 Q4-WK1352201530/09/2015

    I would like to create a full year for each day of show week quarter Qtr/year/QtrWk, exercise, exercise from 01/10/2015-09 30, 2016.

    Appreciate your help and advice.

    Thank you

    AMan

    Hi AMan,

    can't answer your question with a detailed requirement.

    No doubt a whole year can be created in a SQL statement (CONNECT BY or the recursive subquery factoring) and is not a problem.

    But I would like to ask you the following:

    • How do you get the week number? There might be a different way to look at it (ISO or not, week starting with the Sun, Mon or the first day of the year, etc.)
    • The same question can be applied to the financial week. When it's the first week? 01/10/2015 until 10/07/2015?

    You can have a quick glance at the definition of different week in 'Model of Format of Date' on this page:

    https://docs.Oracle.com/CD/B28359_01/server.111/b28286/sql_elements004.htm

    Check the difference between WW and IW.

    Kind regards.

    Alberto

Maybe you are looking for