Sort date by week AND year

How do I sort a bunch of date by week number and year? I have a bunch of date in the format IW-YYYY such as
45-2010
46-2010
47-2010
48-2010
49-2010
50-2010
51-2010
52-2010
01-2011
02-2011
If I sort by week, I get the following which is not what I want. I want the above.
01-2011
02-2011
45-2010
46-2010
47-2010
48-2010
49-2010
50-2010
51-2010
52-2010
Thanks,
SM

do you happen to have the underlying dates as well as the iw-yyyy format? If so, then it's just a matter of:
order by trunc(actual_date_col, 'iw')If not, then you're going to have to do:
order by to_number(substr(date_in_iw_format_col, 4)), to_number(substr(date_in_iw_format_col, 1, 2))eg:
with sample_data as (select '45-2010' date_in_iw_format_col from dual union all
                     select '46-2010' date_in_iw_format_col from dual union all
                     select '47-2010' date_in_iw_format_col from dual union all
                     select '48-2010' date_in_iw_format_col from dual union all
                     select '49-2010' date_in_iw_format_col from dual union all
                     select '50-2010' date_in_iw_format_col from dual union all
                     select '51-2010' date_in_iw_format_col from dual union all
                     select '52-2010' date_in_iw_format_col from dual union all
                     select '01-2011' date_in_iw_format_col from dual union all
                     select '02-2011' date_in_iw_format_col from dual)
select *
from   sample_data
order by to_number(substr(date_in_iw_format_col, 4)),
         to_number(substr(date_in_iw_format_col, 1, 2));
DATE_IN_IW_FORMAT_COL
45-2010             
46-2010             
47-2010             
48-2010             
49-2010             
50-2010             
51-2010             
52-2010             
01-2011             
02-2011             

Similar Messages

  • Any Function Module to get date by passing week and year

    Hi,
       Is there any Function Module available to get date by passing week and year. For example, Week 24, Year 2005 and you get the date.
    Regards,
    Mira

    WEEK_GET_FIRST_DAY
    pass 'YYYYWW' (200524) to WEEK parameter it will give you the week start date
    Raja

  • I want to keep date special month and year.

    I want to keep date special month and year.
    Example in sqlplus
    insert into area(input_time) values(to_date('MAR-04','MON-YY'));
    and select
    select input_time from area;
    the result
    01 MAR 2004
    in Oracle
    01-MAR-2004 12:00:00 AM
    I don't keep day, but I want to keep month and year.
    Can you do?
    Thanks.

    An Oracle date column must contain a date down to the second. If you don't specify a day or a time, it will default to the first of the month and midnight.
    If you want a column to only contain values at the beginning of a particular month, you can create a CHECK constraint that verifies that
    input_time = trunc( input_time, 'MM' )Justin
    Distributed Database Consulting, Inc.
    http://www.ddbcinc.com/askDDBC

  • Monday's Date  of the given week  and year

    Hi gurus,
    I have the data for week ( eg.52 ) and year ( eg. 08 ) and I need to get the date of the Monday of that week (21-DEC-2008...i.e 52th week of 2008 Year.)
    Pls let me knw the sql statement solution for this ..
    THanks,
    shashi..

    Hi, Shashi,
    There's a bug in what I posted before.
    Do this instead:
    TRUNC ( TO_DATE ( '01-Jul-' || :year_txt
                    , 'DD-Mon-YYYY'
          , 'IY'
          ) + (7 * (:week_val - 1))The only change is in TO_DATE.
    Here's the problem.
    TO_DATE won't work with ISO years; it only takes calendar years.
    When you don't specify a day and month in TO_DATE (like I originally did) it defaults to the first day of the current month. So TO_DATE ('2011', 'YYYY') returns October 1, 2011 when the current month is October, and it returns January 1, 2011 when the current month is January.
    January 1, 2011, however, is in ISO year 2010. (ISO 2011 begins on the Monday closest to January 1, which is January 3, 2011). My original solution, therefore, would return dates from ISO year 2010 when anyone asked for year 2011 and they happend to be running the program during January.
    Specifying a full date makes the expression behave the same regardless of when it is run.

  • Store value less than current week and year.

    Hi All,
    I have a table called BACKLOG whose structure is like this
    ITEM_NUMBER   YEAR_WEEK   MS
    1N58          2012-WK02   01/15/2012
    1N58          2011-WK02   01/15/2011current from my procedure i am storing the value in my new table which is less than current week. But i want to store the value less than current week and 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 procedure code both the value is storing in my BACKLOG_LT_CW table but if you see the data of BACKLOG table you will find YEAR_WEEk 2012-WK02 falls in next year but still it is storing in my table because i have taken IW only. Please help me to get the soln
    Thanks in advance
    Regards

    Are you looking for this?
    where to_char(MS, 'YYYYIW')<to_char(sysdate, 'YYYYIW');

  • How to sort data from database and display in combobox in required order

    Hi evrybody, it makes me sick i need to get data form database and fill combobox in required order (last item added to database - last firstName) actualy i already did that, but i'm useing DefaultComboBoxModel and all records are sorted in alphabetical order. Thanks very much for any help

    The items in the JComboBox are displayed in the order in which you add them to the combo box. Use an order by clause in your SQL statement.
    Don't [url http://forum.java.sun.com/thread.jsp?forum=54&thread=516608]crosspost.

  • Date into Mon and year

    Dear All,
    I have date column.I have to display Month name and Year.
    E.g
    NOV 2011
    DEC 2011
    How will get this could you pls send me Syntex
    Thanks

    Hi Govind,
    There are many ways to do,
    1. you can use evaluate function to do that.
    2. simple and better way is to overide the date formate of thet coloumn with the custom and then type MMM YYYY
    Hope this helps you...

  • Aggregated data for quatrter and year

    Hi,
         I have created a input schedule by using any by any template, I have account on row and time dimension on column, I input some test data for each month for some accounts, it work ok and I send the data successfully.
        I create another report using Evdre with the  row and column structure, I set both member set as "SELF AND DEP", however, when I run the report, I notice that the Quarter data and the year data is not correct, For example, Q1 data should the  summary of Jan,FEB and MAR, instead it show the same data as MAR, while the year data show the same data as Q4.
       Anybody know hot to fix this?
    Thanks

    I did another test as following:
    I copy a appshell from SAP standard AppShell, I generated some test data for each month using input schedule, then I build a evdre report, it work perfect,if i  choose PERIODIC, I get the quarter and year data summary the month or quarter, if I choose YTD, I get the year to date data,
    then I copy the my master data to  replace the orginal master data, I generated some test data, this time the report only show the YTD data no mater I choose PERIODC OR YTD for measure in CV.   This is verystrange.

  • Determining last date if period and year are given

    hi,
    what is the code if  the period and year are given and  to  print the  last date of that particular period and
    year.

    suppos period is perio = '2006002'
    Data sdate type sy-datum.
    Data edate type sy-datum.
    mm = perio+5(2).
    yy   = perio+0(4).
      concatenate yy mm '01' into sdate.
      call function 'SG_PS_GET_LAST_DAY_OF_MONTH'
        exporting
          day_in            = sdate
        importing
          last_day_of_month = edate.
    edate is your lst day of that period month.
    \[removed by moderator\]
    Regards
    Rajesh
    Edited by: Jan Stallkamp on Jul 21, 2008 2:35 PM

  • Get start and end week date by month and year

    How I need the week is by this -- a week always starts on a sunday so for feburary it will be
    start week end week
    12-02-1 12-02-04
    12-02-05 12-02-11
    12-02-12 12-02-18
    12-02-19 12-02-25
    12-02-26 12-29-29
    So because the first week does not start on a sunday it goes from
    1(wednesday) to 4th(saturday) Just like a calendar.
    Thanks in advance
    so far from Solomons help I have this
    This is for january
    WITH t AS (
    SELECT TRUNC(TRUNC(to_date('01-01-12','MM-DD-YY'),'YYYY') -1,'W') + (LEVEL-1 ) * 7 week_start_date
    FROM DUAL
    CONNECT BY LEVEL <= 53
    SELECT week_start_date,
    LEAST(LAST_DAY(to_date('01-01-12','MM-DD-YY')),week_start_date + 6) week_end_date
    FROM t
    WHERE week_start_date >= TRUNC(to_date('01-01-12','MM-DD-YY'),'MM')
    AND week_start_date < ADD_MONTHS(TRUNC(to_date('01-01-12','MM-DD-YY'),'MM'),1)
    start week end week
    12-01-02     12-01-08
    12-01-09     12-01-15
    12-01-16     12-01-22
    12-01-23     12-01-29
    12-01-30     12-01-31

    If week satrts Sunday:
    WITH t AS (
               SELECT TRUNC(TRUNC(&dt,'MM') + 1,'IW') - 1 + (LEVEL - 1) * 7 week_start_date
                FROM  DUAL
                CONNECT BY TRUNC(TRUNC(&dt,'MM') + 1,'IW') - 1 + (LEVEL - 1) * 7 <= LAST_DAY(&dt)
    SELECT  GREATEST(week_start_date,TRUNC(&dt,'MM')) week_start_date,
            LEAST(LAST_DAY(&dt),week_start_date + 6) week_end_date
      FROM  t
      WHERE week_start_date <= LAST_DAY(&dt)
    /For example:
    SQL> SET VERIFY OFF
    SQL> DEFINE dt="DATE '2012-01-17'" -- January
    SQL> WITH t AS (
      2             SELECT TRUNC(TRUNC(&dt,'MM') + 1,'IW') - 1 + (LEVEL - 1) * 7 week_start_date
      3              FROM  DUAL
      4              CONNECT BY TRUNC(TRUNC(&dt,'MM') + 1,'IW') - 1 + (LEVEL - 1) * 7 <= LAST_DAY(&dt)
      5            )
      6  SELECT  GREATEST(week_start_date,TRUNC(&dt,'MM')) week_start_date,
      7          LEAST(LAST_DAY(&dt),week_start_date + 6) week_end_date
      8    FROM  t
      9    WHERE week_start_date <= LAST_DAY(&dt)
    10  /
    WEEK_STAR WEEK_END_
    01-JAN-12 07-JAN-12
    08-JAN-12 14-JAN-12
    15-JAN-12 21-JAN-12
    22-JAN-12 28-JAN-12
    29-JAN-12 31-JAN-12
    SQL> SET VERIFY OFF
    SQL> DEFINE dt="DATE '2012-02-25'" -- February
    SQL> WITH t AS (
      2             SELECT TRUNC(TRUNC(&dt,'MM') + 1,'IW') - 1 + (LEVEL - 1) * 7 week_start_date
      3              FROM  DUAL
      4              CONNECT BY TRUNC(TRUNC(&dt,'MM') + 1,'IW') - 1 + (LEVEL - 1) * 7 <= LAST_DAY(&dt)
      5            )
      6  SELECT  GREATEST(week_start_date,TRUNC(&dt,'MM')) week_start_date,
      7          LEAST(LAST_DAY(&dt),week_start_date + 6) week_end_date
      8    FROM  t
      9    WHERE week_start_date <= LAST_DAY(&dt)
    10  /
    WEEK_STAR WEEK_END_
    01-FEB-12 04-FEB-12
    05-FEB-12 11-FEB-12
    12-FEB-12 18-FEB-12
    19-FEB-12 25-FEB-12
    26-FEB-12 29-FEB-12
    SQL> SET VERIFY OFF
    SQL> DEFINE dt="DATE '2012-03-07'" -- March
    SQL> WITH t AS (
      2             SELECT TRUNC(TRUNC(&dt,'MM') + 1,'IW') - 1 + (LEVEL - 1) * 7 week_start_date
      3              FROM  DUAL
      4              CONNECT BY TRUNC(TRUNC(&dt,'MM') + 1,'IW') - 1 + (LEVEL - 1) * 7 <= LAST_DAY(&dt)
      5            )
      6  SELECT  GREATEST(week_start_date,TRUNC(&dt,'MM')) week_start_date,
      7          LEAST(LAST_DAY(&dt),week_start_date + 6) week_end_date
      8    FROM  t
      9    WHERE week_start_date <= LAST_DAY(&dt)
    10  /
    WEEK_STAR WEEK_END_
    01-MAR-12 03-MAR-12
    04-MAR-12 10-MAR-12
    11-MAR-12 17-MAR-12
    18-MAR-12 24-MAR-12
    25-MAR-12 31-MAR-12
    SQL> SY.
    Edited by: Solomon Yakobson on Jan 12, 2012 1:19 PM

  • FMS for Udf based on Posting Date + 4 weeks and +4 weeks excluding weekends

    Hello,
    Requirement 1
    Have made a udf in the Sales Order Header as Manfacture Drawing Rel Date U_FDRDT in which I want that if a Sales Order posting date is 15/12/2010 then this udf should automatically update with the + 4 weeks date i.e 14/01/2011
    Requirement 2
    Another UDF is also made as U_FRDWDT in which I want that if the Sales Order posting date is 15/12/2010 then this udf should automatically update + 4 weeks EXCLUDING Fridays and Saturdays which is there in the 4 weeks hence the result should be e.g 27/01/2010 which is excluding Fridays and Saturdays.The requirement is as such that Friday and Saturday are holidays for the company and the client wants to have this field updated on + 4 weeks basis but it should not consider Friday and Saturday as its holiday.
    Also note in SAP base setup Holiday Master Weekend is set from Friday to Saturday. Will this help in any way of updating the field or through formated search.
    Kindly advise what best solution can be given
    Regards,
    Soni

    Hi,
    One question per thread. This is forum rule.
    Here is FMS for the 1st one:
    SELECT DateAdd(WW,4,$[ORDR.DocDate\])
    Please post another for the 2nd.
    Thanks,
    Gordon

  • Date to Month and Year

    Hi Guru's
    We have a requirement of taking the input in date/month/year format in the DSO and the output to be displayed in the report is in month/year in the report.
    We dont want to do any changes/modification in the DSO level.
    Any ideas on how to do it.
    Thanks
    Krishna

    Hi,
    Using the following code you can do it, but in columns you want to display the month, it is mot possible, i.e. JAN-08, FEB-08 like that it is not possible without adding new InfoObject in DSO. It works for Text variables.
    'ZDATE_U' =   Normal Variable for Calday user entry.
    'ZMONTH_E'. = Customet Exit for Calmonth
    when 'ZMONTH_E'.
    DATA: zdtt(8) TYPE c,
                w_mnth(3) TYPE c.
          LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZDATE_U'.
            CLEAR l_s_range.
            w_day = loc_var_range-low+6(2).
            w_mon  = loc_var_range-low+4(2).
            w_year = loc_var_range-low+0(4).
            CASE w_mon.
              WHEN '01'.
                w_mnth = 'JAN'.
              WHEN '02'.
                w_mnth = 'FEB'.
              WHEN '03'.
                w_mnth = 'MAR'.
              WHEN '04'.
                w_mnth = 'APR'.
              WHEN '05'.
                w_mnth = 'MAY'.
              WHEN '06'.
                w_mnth = 'JUN'.
              WHEN '07'.
                w_mnth = 'JUL'.
              WHEN '08'.
                w_mnth = 'AUG'.
              WHEN '09'.
                w_mnth = 'SEP'.
              WHEN '10'.
                w_mnth = 'OCT'.
              WHEN '11'.
                w_mnth = 'NOV'.
              WHEN '12'.
                w_mnth = 'DEC'.
            ENDCASE.
           CONCATENATE w_day '-' w_mnth '-' w_year INTO zdtt.
           CONCATENATE w_mnth '-' w_year INTO zdtt.
            CONCATENATE w_mnth '-' w_year+2(2) INTO zdtt.
            l_s_range-low = zdtt.
            l_s_range-sign = 'I'.
            l_s_range-opt = 'EQ'.
            APPEND l_s_range TO e_t_range.
          ENDLOOP.
    Thanks
    Reddy

  • Convert Date to Month and Year

    Hi,
    I am currently using Function Module CONVERSION_EXIT_LDATE_OUTPUT to convert a date example 20090901 to 01 September 2009.
    Using z_date+3 then zdate becomes September 2009 which is what i require.
    In the report when I actually sort by month it doesn't really work well example below.
    September 2009
    September 2010
    October 2009
    October 2010
    November 2009
    November 2010
    It should really be like the below. I dont want to covert the date to 09.2009 i want it to be September 2009 hence i have taken this approach. Any help would be appreciated.
    September 2009
    October 2009
    November 2009
    September 2010
    October 2010
    November 2010
    Regards
    Adeel

    as you are storing it into one string, its just sorting on the basis of string comparison which starts from first later.
    store them to two different fields.
    like
    data : BEGIN OF gt_dt OCCURS 0,
            mnth type string,
            year type n LENGTH 4,
           end of gt_dt.
    DATA : s1 TYPE string,
          i1 type i.
    *lets say here you get the values to this table like:
    s1 = '01 September 2009'.
    shift s1 by 3 PLACES left.
    i1 = strlen( s1 ).
    i1 = i1 - 4.
    gt_dt-mnth = s1+0(i1).
    shift s1 by i1 PLACES left.
    gt_dt-year = s1.
    APPEND gt_dt.
    s1 = '01 September 2010'.
    shift s1 by 3 PLACES left.
    i1 = strlen( s1 ).
    i1 = i1 - 4.
    gt_dt-mnth = s1+0(i1).
    shift s1 by i1 PLACES left.
    gt_dt-year = s1.
    APPEND gt_dt.
    s1 = '01 October 2009'.
    shift s1 by 3 PLACES left.
    i1 = strlen( s1 ).
    i1 = i1 - 4.
    gt_dt-mnth = s1+0(i1).
    shift s1 by i1 PLACES left.
    gt_dt-year = s1.
    APPEND gt_dt.
    s1 = '01 October 2010'.
    shift s1 by 3 PLACES left.
    i1 = strlen( s1 ).
    i1 = i1 - 4.
    gt_dt-mnth = s1+0(i1).
    shift s1 by i1 PLACES left.
    gt_dt-year = s1.
    APPEND gt_dt.
    now you can sort as you wish.

  • Fiscal date DD-MON and year end looking forward

    Trying to figure out the best way to look forward to the new year and back to the old year when comparing sysdate to a table column fiscal that is formatted DD-MON. The process works perfectly during the year, since is uses current year when comparing data. But at the end of the year, we do not see anything past 12/31 or before 1/1 in a new year. Any ideas? I was thinking of declaring a couple of values and based on sysdate Sep, Oct, Nov, Dec to add 1 year to Fiscal months Jan, Feb, Mar, Apr so they appear as the next year and vice versa for the first months of a new year, subtract one year from the fiscals to indicate previous year. Has anyone worked on something like this in the past?

    Look in to this, i am substituting values for the year, so you will get idea.
    The fisccal date you have is month end all the time or it is all the dates?
    SQL> ed
    Wrote file afiedt.buf
      1  WITH T AS
      2    (SELECT '01-DEC' dt  FROM dual
      3     UNION ALL
      4    SELECT  '05-JUL'   FROM dual
      5     UNION ALL
      6    SELECT  '10-JAN'     FROM dual
      7     UNION ALL
      8    SELECT  '30-MAR'    FROM dual
      9     )
    10  --ens of data
    11  SELECT TO_DATE(dt||'-'||&y,'DD-MON-YYYY') fisc_date,
    12         TO_DATE(dt||'-'||&y,'DD-MON-YYYY')+180 NEW_DATE,
    13      ADD_MONTHS(TO_DATE(dt||'-'||&y,'DD-MON-YYYY'),6)
    14* FROM T
    SQL> /
    Enter value for y: 2008
    old  11: SELECT TO_DATE(dt||'-'||&y,'DD-MON-YYYY') fisc_date,
    new  11: SELECT TO_DATE(dt||'-'||2008,'DD-MON-YYYY') fisc_date,
    Enter value for y: 2008
    old  12:        TO_DATE(dt||'-'||&y,'DD-MON-YYYY')+180 NEW_DATE,
    new  12:        TO_DATE(dt||'-'||2008,'DD-MON-YYYY')+180 NEW_DATE,
    Enter value for y: 2008
    old  13:     ADD_MONTHS(TO_DATE(dt||'-'||&y,'DD-MON-YYYY'),6)
    new  13:     ADD_MONTHS(TO_DATE(dt||'-'||2008,'DD-MON-YYYY'),6)
    FISC_DATE NEW_DATE  ADD_MONTH
    01-DEC-08 30-MAY-09 01-JUN-09
    05-JUL-08 01-JAN-09 05-JAN-09
    10-JAN-08 08-JUL-08 10-JUL-08
    30-MAR-08 26-SEP-08 30-SEP-08

  • No service from O2 - no data for weeks and get cut off on customer support

    After a couple of weeks without a reliable data service and poor phone connections in the Bridport area I have tried to contact support. As usual they are experiencing the same higher than normal calls so I have been listening to your poor quality music for 12 minutes or so before you just dropped the line. I am 12 minutes into the second call and you cut me off again.  This is not customer service it is a disgrace. There is also no chat available. Pathetic that a communications company can't communicate! I have already had to take out a monthly contract with another supplier to be able to keep my business in touch. I could be looking to move away from O2 very soon

    David_Smith wrote:
    After a couple of weeks without a reliable data service and poor phone connections in the Bridport area I have tried to contact support. As usual they are experiencing the same higher than normal calls so I have been listening to your poor quality music for 12 minutes or so before you just dropped the line. I am 12 minutes into the second call and you cut me off again.  This is not customer service it is a disgrace. There is also no chat available. Pathetic that a communications company can't communicate! I have already had to take out a monthly contract with another supplier to be able to keep my business in touch. I could be looking to move away from O2 very soonI know its not going to help but i have EXACTLY the same issue and it also has been going on for weeks!! And you keep getting fed the same ole rubbsih from robots that work at O2!! Its not acceptable and the frustrating part is that we as consumers are completely useless!!  These Companies need SLA (Service Level Agreements) put in place by the regulator that will put a rocket up their rear & make them get things done in a "reasonable" time or get fined!! We that are affected are paying for a service that is simply not working, its not just a one off, it happens quite regular in some areas like mine!!!! Its probably not the answer you want to hear but its poor & big company syndrome lets them get away with it, you are the sheep, they are the wolf!!!!

Maybe you are looking for