Adding months to a date

I`m setting up ranges for searching a database. I plan to use
a combobox for choosing 'year', 'month', 'week' as ranges from a
given date. I have one datefield for the start date, and want
another one to reflect the date of the range limit.
In other words, I want to select '1 month', '2 months' etc.
from a combobox and calculate the end date.
Anyone ?

"toft" <[email protected]> wrote in message
news:gnaor6$hgf$[email protected]..
> I`m setting up ranges for searching a database. I plan
to use a combobox
> for
> choosing 'year', 'month', 'week' as ranges from a given
date. I have one
> datefield for the start date, and want another one to
reflect the date of
> the
> range limit.
>
> In other words, I want to select '1 month', '2 months'
etc. from a
> combobox
> and calculate the end date.
http://flexdiary.blogspot.com/2008/05/working-with-dates-in-as3.html

Similar Messages

  • Is there a completely reliable method of adding months to a date in ABAP?

    Does anyone know of a completely reliable and consistent ABAP function module that can be used to add months to a date.  One that will always get the correct last day of the month when requested to add 1 month to the last day of the previous month.  Something as reliable as using the ADD_MONTHS function in Oracle SQL.  I don't want to use any of the specific 'get last day of the month' function modules since the start date may not necessarily be the last day of a month.
    In the past I have trusted the following.  Now they have betrayed me. 
    MONTHS_PLUS_DETERMINE  
    Correctly provides 28.02.09 when adding 1 months to 31.01.09.
    Incorrectly gives me 28.03.09 instead of 31.03.09 when adding 1 month to 28.02.09
    RP_CALC_DATE_IN_INTERVAL and RP_CALC_DATE_IN_INTERVAL_SG
    Both incorrectly give me 01.03.09 when asked to add 1 month to 31.01.09.
    Both incorrectly give me 28.03.09 when asked to add 1 month to 28.02.09.
    We're on ECC6.

    >
    Suhas Saha wrote:
    > Hello Christine,
    >
    > Did you check the method ADD_MONTHS_TO_DATE of the class CL_HRPAD_DATE_COMPUTATIONS ?
    >
    >
    > *     Adds No. of Months to Date
    >       TRY.
    >           CALL METHOD cl_hrpad_date_computations=>add_months_to_date
    >             EXPORTING
    >               start_date = sy-datum
    >               months     = l_v_month
    >             RECEIVING
    >               date       = l_v_date.
    >         CATCH cx_hrpa_violated_postcondition .
    >       ENDTRY.
    >
    >
    > I dont have any idea how ADD_MONTHS function in Oracle SQL works, though ):
    >
    > Hope this helps.
    >
    > BR,
    > Suhas
    That also sometimes works.....but adding 1 month to 28.02.2009 gives me 28.03.2009 and adding 1 month to 29.02.2008 gives me 29.03.2008.
    This is how to use ADD_MONTHS in Oracle SQL - a bit naughty since you have to use native SQL to do it but it ALWAYS seems to work.  I pass a date, month number and + or - into the function module.
    * For use with class based exception CX_SY_OPEN_SQL_DB.
    DATA:
      ex_check_os       TYPE REF TO cx_sy_open_sql_db,
      ex_check_rs       TYPE REF TO cx_sy_native_sql_error,
      ex_result(200)    TYPE C,
      ex_text           TYPE STRING,
      lv_new_date       TYPE datum,
      lv_old_date       TYPE datum,
      lv_months         TYPE I.
      lv_old_date = iv_date.
      lv_months = iv_months.
      IF iv_sign = '-'.
         lv_months = lv_months * -1.
      ENDIF.
      TRY.
        EXEC SQL.
          SELECT to_char(add_months(to_date(:lv_old_date,'YYYYMMDD'),:lv_months),'YYYYMMDD')
          INTO :lv_new_date
          FROM sys.dual a
        ENDEXEC.
        CATCH cx_sy_native_sql_error INTO ex_check_rs.
        ex_text = ex_check_rs->get_text( ).
        ev_error_message = ex_text.
        ev_return = 4.
      ENDTRY.
      ev_return = 0.
      ev_date = lv_new_date.

  • Function module for adding number of months to the date

    Hi,
    Is there any function module to get the date by adding number of months...
    Regards,
    Yadagiri

    Normally all SAP FM will return that value. As 1 month in general means 30 days only.
    You can check the code in this link for logig of 31 -
    add month in the date to find next date
    Regards,
    Amit

  • Adding months to date

    I need to add 6 months to current date. Also, may be days or years also. Can anyone let me know is there any function module which can be used to achieve the functionality.

    Hi jimmy,
    here is the code for FM "MONTH_PLUS_DETERMINE".
    ========================================================
    FUNCTION MONTH_PLUS_DETERMINE.
    *"Lokale Schnittstelle:
    *"       IMPORTING
    *"             MONTHS
    *"             OLDDATE LIKE SY-DATUM
    *"       EXPORTING
    *"             NEWDATE LIKE SY-DATUM
      DATA:       BEGIN OF DAT,
                  JJJJ(4) ,
                  MM(2) ,
                  TT(2) ,
                  END OF DAT,
                  BEGIN OF HDAT,
                  JJJJ(4) ,
                  MM(2) ,
                  TT(2) ,
                  END OF HDAT,
                  NEWMM TYPE P,
                  DIFFJJJJ TYPE P.
      WRITE:  OLDDATE+0(4) TO DAT-JJJJ,
              OLDDATE+4(2) TO  DAT-MM,
              OLDDATE+6(2) TO  DAT-TT.
      DIFFJJJJ =   ( DAT-MM + MONTHS - 1 ) DIV 12.
      NEWMM    =   ( DAT-MM + MONTHS - 1 ) MOD 12 + 1.
      DAT-JJJJ = DAT-JJJJ +  DIFFJJJJ.
      IF NEWMM < 10.
        WRITE '0' TO  DAT-MM+0(1).
        WRITE NEWMM TO  DAT-MM+1(1).
      ELSE.
        WRITE NEWMM TO  DAT-MM.
      ENDIF.
      IF DAT-TT > '28'.
        HDAT-TT = '01'.
        NEWMM   = ( DAT-MM  )  MOD 12 + 1.
        HDAT-JJJJ = DAT-JJJJ + ( (  DAT-MM ) DIV 12 ).
        IF NEWMM < 10.
          WRITE '0' TO HDAT-MM+0(1).
          WRITE NEWMM TO HDAT-MM+1(1).
        ELSE.
          WRITE NEWMM TO HDAT-MM.
        ENDIF.
        IF DAT-TT = '31'.
          NEWDATE = HDAT.
          NEWDATE = NEWDATE - 1.
        ELSE.
          IF DAT-MM = '02'.
            NEWDATE = HDAT.
            NEWDATE = NEWDATE - 1.
          ELSE.
            NEWDATE = DAT.
          ENDIF.
        ENDIF.
      ELSE.
        NEWDATE = DAT.
      ENDIF.
    ENDFUNCTION.
    =========================================================

  • How can i add one month to a date variable ?

    Unlike the week which always 7 days, month may change from 28 to
    31, so how can i add one month to a date variable ? Thanks in
    advance.

    Adding 365 won't always work because of leap years.
    You could use the following SQL statement:
    SELECT
         TO_DATE(
              TO_CHAR(TO_DATE(SYSDATE), 'DD-MON-') ||
                (TO_CHAR(TO_DATE(SYSDATE), 'YYYY') + 1)) NEXT_YEAR
    FROM
         dual
    NEXT_YEAR
    08-JAN-03Or you could create your own function. You would use this
    exactly like add_months:
    CREATE OR REPLACE FUNCTION add_years
         (v_date DATE, num_years NUMBER)
    RETURN DATE AS
         v_year DATE;
    BEGIN
         v_year := TO_DATE(
                   TO_CHAR(TO_DATE(SYSDATE), 'DD-MON-') ||
                         (TO_CHAR(TO_DATE(SYSDATE), 'YYYY')
                                             + num_years));
         RETURN v_year;
    END;     
    SELECT
         add_years(SYSDATE, 1) new_year
    FROM
         dual
    NEW_YEAR 
    08-JAN-03
    SELECT
         add_years(SYSDATE, -1) new_year
    FROM
         dual
    NEW_YEAR 
    08-JAN-01

  • Week and month calculation from date column

    I have 3 column data like
    with tab as
      select 'Topshop' brand, '10-JUL-11' deliverydate, '100' qty from dual union all
      select 'Topshop' brand, '10-JUL-11' deliverydate, '400' qty from dual union all
      select 'NewSita' brand, '11-JUL-11' deliverydate, '200' qty from dual union all
      select 'LaGress' brand, '12-JUL-11' deliverydate, '300' qty from dual union all
      select 'LaGress' brand, '10-AUG-11' deliverydate, '100' qty from dual union all
      select 'LaGress' brand, '11-AUG-11' deliverydate, '200' qty from dual union all
      select 'Topshop' brand, '12-AUG-11' deliverydate, '300' qty from dual union all
      select 'NewSita' brand, '10-SEP-11' deliverydate, '100' qty from dual union all
      select 'Topshop' brand, '11-SEP-11' deliverydate, '200' qty from dual union all
      select 'NewSita' brand, '12-SEP-11' deliverydate, '300' qty from dual
    ) select * from tabI need to convert it into 4 columns
    Brand | Month | Week(start date) | Qty (sum)
    Please let me know what are the options that i have, especially the date-time calculation functions available to solve such problems.
    Thanks
    w\

    with tab as
      select 'Topshop' brand, to_date('10-JUL-11', 'dd-mon-yy') deliverydate, '100' qty from dual union all
      select 'Topshop' brand, to_date('10-JUL-11', 'dd-mon-yy') deliverydate, '400' qty from dual union all
      select 'NewSita' brand, to_date('11-JUL-11', 'dd-mon-yy') deliverydate, '200' qty from dual union all
      select 'LaGress' brand, to_date('12-JUL-11', 'dd-mon-yy') deliverydate, '300' qty from dual union all
      select 'LaGress' brand, to_date('10-AUG-11', 'dd-mon-yy') deliverydate, '100' qty from dual union all
      select 'LaGress' brand, to_date('11-AUG-11', 'dd-mon-yy') deliverydate, '200' qty from dual union all
      select 'Topshop' brand, to_date('12-AUG-11', 'dd-mon-yy') deliverydate, '300' qty from dual union all
      select 'NewSita' brand, to_date('10-SEP-11', 'dd-mon-yy') deliverydate, '100' qty from dual union all
      select 'Topshop' brand, to_date('11-SEP-11', 'dd-mon-yy') deliverydate, '200' qty from dual union all
      select 'NewSita' brand, to_date('12-SEP-11', 'dd-mon-yy') deliverydate, '300' qty from dual
    ), week_data as (
       select brand
       ,      to_char(deliverydate, 'Month', 'nls_date_language=american') MON
       ,      trunc(deliverydate, 'day') WEEK_START
       ,      sum(TO_NUMBER(qty)) SUM_QTY
       from   tab
       group by brand
       ,      to_char(deliverydate, 'Month', 'nls_date_language=american')
       ,      trunc(deliverydate, 'day')
    ), weeks as (
       select
       w.week_min + (level - 1) * 7 week_start
       from (
          select min(week_start) week_min, max(week_start) week_max
          from week_data
       ) w, dual
       connect by level <= 1 + (w.week_max - w.week_min) / 7
    select
    week_data.brand,
    weeks.week_start,
    nvl(week_data.sum_qty,0) sum_qty
    from week_data
    partition by (brand)
    right outer join weeks
    on (weeks.week_start = week_data.week_start)
    order by
    weeks.week_start,
    week_data.brand;You construct a set of records of all "week_start" dates from your minimum to your maximum (classic connect by level trick.)
    You use a partitioned outer join to fill in the gaps in your sparse data (see doc example [url http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_10002.htm#i2177515]here.
    Hope that helps you :-)
    (PS. I dislike implicit conversions - that is the only reason I have added a TO_NUMBER to your code within the sum() ;-) )
    (PPS. Actually you probably should have asked this in a new question - I believe it is not really good forum etiquette to continue in a thread that has already been answered...)

  • Monthly Time Sheet Dates

    I am building a monthly time sheet. I need all the dates to populate once the initial date is chosen by the user. I have this working properly for each month that has 31 days. For months with 30 days, it populates the first day of the next month. How do I make sure that the only the dates for the month selected are populated?
    Any help is appreciated!
    Tracy

    Hello!
    Thank you for your response, but I can not get it to function properly.
    I added a text field to calculate the number of days.  Here is the script I placed in the text field:
    form1.commentSub.NoofDays::calculate - (FormCalc, client)
    var StartDate = Ref(SubPg1.tableSub.Table1.Row1.Date1)
    var EndDate = Ref(SubPg1.tableSub.Table1.Row2.Date2)
    var MonthStart = Date2Num(Date1.formattedValue, "MM/DD/YYYY")
    var nMonth = Num2Date(MonthStart, "MM")                                                                                                     ; Current Month
    var nDay = Num2Date(MonthStart, "DD")                                           ; Current Day
    var nYear = Num2Date(MonthStart, "YYYY")                                        ; Current Year
    var nFirstDay = Date2Num(Concat(nMonth,"01", nYear), "MM/DD/YYYY")        ; First Day in the current month
    var nLastDay
    var nCountDays = nFirstDay
    StartDate.rawValue = Num2Date(nFirstDay, "MM/DD/YYYY") ; Set first day of month as start date
    ; Determine number of days in the current month
    var nMonthCheckStart = Num2Date(nFirstDay, "MM")
    var nMonthCheckEnd
    for i=0 upto 31 step 1 do
              nCountDays = nCountDays + 1
              nMonthCheckEnd = Num2Date(nCountDays , "MM")
              if(nMonthCheckStart == nMonthCheckEnd) then
                        nLastDay = nCountDays
              endif
    endfor
    EndDate.rawValue = Num2Date(nLastDay, "MM/DD/YYYY") ; Set lastday of month as end date
    $.rawValue = Num2Date(nLastDay, "DD") ; Set number of days this month
    In the date field where the user selects the date. I have this:
    form1.SubPg1.tableSub.Table1.Row1.Date1::exit - (FormCalc, client)
    var dayNum = Date2Num($.formattedValue,"MM/DD/YY")
    Row2.Date2.rawValue = Num2Date(dayNum+1,"MM/DD/YY")
    Row3.Date3.rawValue = Num2Date(dayNum+2,"MM/DD/YY")
    Row4.Date4.rawValue = Num2Date(dayNum+3,"MM/DD/YY")
    Row5.Date5.rawValue = Num2Date(dayNum+4,"MM/DD/YY")
    Row6.Date6.rawValue = Num2Date(dayNum+5,"MM/DD/YY")
    Row7.Date7.rawValue = Num2Date(dayNum+6,"MM/DD/YY")
    Row8.Date8.rawValue = Num2Date(dayNum+7,"MM/DD/YY")
    Row9.Date9.rawValue = Num2Date(dayNum+8,"MM/DD/YY")
    Row10.Date10.rawValue = Num2Date(dayNum+9,"MM/DD/YY")
    Row11.Date11.rawValue = Num2Date(dayNum+10,"MM/DD/YY")
    Row12.Date12.rawValue = Num2Date(dayNum+11,"MM/DD/YY")
    Row13.Date13.rawValue = Num2Date(dayNum+12,"MM/DD/YY")
    Row14.Date14.rawValue = Num2Date(dayNum+13,"MM/DD/YY")
    Row15.Date15.rawValue = Num2Date(dayNum+14,"MM/DD/YY")
    Row16.Date16.rawValue = Num2Date(dayNum+15,"MM/DD/YY")
    Row17.Date17.rawValue = Num2Date(dayNum+16,"MM/DD/YY")
    Row18.Date18.rawValue = Num2Date(dayNum+17,"MM/DD/YY")
    Row19.Date19.rawValue = Num2Date(dayNum+18,"MM/DD/YY")
    Row20.Date20.rawValue = Num2Date(dayNum+19,"MM/DD/YY")
    Row21.Date21.rawValue = Num2Date(dayNum+20,"MM/DD/YY")
    Row22.Date22.rawValue = Num2Date(dayNum+21,"MM/DD/YY")
    Row23.Date23.rawValue = Num2Date(dayNum+22,"MM/DD/YY")
    Row24.Date24.rawValue = Num2Date(dayNum+23,"MM/DD/YY")
    Row25.Date25.rawValue = Num2Date(dayNum+24,"MM/DD/YY")
    Row26.Date26.rawValue = Num2Date(dayNum+25,"MM/DD/YY")
    Row27.Date27.rawValue = Num2Date(dayNum+26,"MM/DD/YY")
    Row28.Date28.rawValue = Num2Date(dayNum+27,"MM/DD/YY")
    Row29.Date29.rawValue = Num2Date(dayNum+28,"MM/DD/YY")
    Row30.Date30.rawValue = Num2Date(dayNum+29,"MM/DD/YY")
    Row31.Date31.rawValue = Num2Date(dayNum+30,"MM/DD/YY")
    Row1.Day1.rawValue = Num2Date(dayNum,"EEE")
    Row2.Day2.rawValue = Num2Date(dayNum+1,"EEE")
    Row3.Day3.rawValue = Num2Date(dayNum+2,"EEE")
    Row4.Day4.rawValue = Num2Date(dayNum+3,"EEE")
    Row5.Day5.rawValue = Num2Date(dayNum+4,"EEE")
    Row6.Day6.rawValue = Num2Date(dayNum+5,"EEE")
    Row7.Day7.rawValue = Num2Date(dayNum+6,"EEE")
    Row8.Day8.rawValue = Num2Date(dayNum+7,"EEE")
    Row9.Day9.rawValue = Num2Date(dayNum+8,"EEE")
    Row10.Day10.rawValue = Num2Date(dayNum+9,"EEE")
    Row11.Day11.rawValue = Num2Date(dayNum+10,"EEE")
    Row12.Day12.rawValue = Num2Date(dayNum+11,"EEE")
    Row13.Day13.rawValue = Num2Date(dayNum+12,"EEE")
    Row14.Day14.rawValue = Num2Date(dayNum+13,"EEE")
    Row15.Day15.rawValue = Num2Date(dayNum+14,"EEE")
    Row16.Day16.rawValue = Num2Date(dayNum+15,"EEE")
    Row17.Day17.rawValue = Num2Date(dayNum+16,"EEE")
    Row18.Day18.rawValue = Num2Date(dayNum+17,"EEE")
    Row19.Day19.rawValue = Num2Date(dayNum+18,"EEE")
    Row20.Day20.rawValue = Num2Date(dayNum+19,"EEE")
    Row21.Day21.rawValue = Num2Date(dayNum+20,"EEE")
    Row22.Day22.rawValue = Num2Date(dayNum+21,"EEE")
    Row23.Day23.rawValue = Num2Date(dayNum+22,"EEE")
    Row24.Day24.rawValue = Num2Date(dayNum+23,"EEE")
    Row25.Day25.rawValue = Num2Date(dayNum+24,"EEE")
    Row26.Day26.rawValue = Num2Date(dayNum+25,"EEE")
    Row27.Day27.rawValue = Num2Date(dayNum+26,"EEE")
    Row28.Day28.rawValue = Num2Date(dayNum+27,"EEE")
    Row29.Day29.rawValue = Num2Date(dayNum+28,"EEE")
    Row30.Day30.rawValue = Num2Date(dayNum+29,"EEE")
    Row31.Day31.rawValue = Num2Date(dayNum+30,"EEE")
    It still adds the beginning date for the following month if the month selected only has 30 days.  
    I am not sure how to attach the file here for you to see.
    Tracy

  • Function to list the month from a date range?

    I would like to know what the function is that would take a look at a date range, and extract the month name
    Here is how I would like it to come out:

    Hello
    The following sample tables are along your original scheme using month name to filter the data.
    2014 (excerpt)
    A1  month
    A2  =MONTHNAME(MONTH(B2))
    A3  =MONTHNAME(MONTH(B3))
    A4  =MONTHNAME(MONTH(B4))
    B1  date
    B2  2013-01-15
    B3  2013-01-20
    B4  2013-01-27
    C1  category
    C2  A
    C3  B
    C4  C
    D1  amount
    D2  100
    D3  50
    D4  20
    January
    A1  category
    A2  A
    A3  B
    A4  C
    A5  D
    A6  E
    A7  F
    A8  G
    A9  H
    B1  totals
    B2  =SUMIFS(2014::D,2014::A,C$1,2014::C,A2)
    B3  =SUMIFS(2014::D,2014::A,C$1,2014::C,A3)
    B4  =SUMIFS(2014::D,2014::A,C$1,2014::C,A4)
    B5  =SUMIFS(2014::D,2014::A,C$1,2014::C,A5)
    B6  =SUMIFS(2014::D,2014::A,C$1,2014::C,A6)
    B7  =SUMIFS(2014::D,2014::A,C$1,2014::C,A7)
    B8  =SUMIFS(2014::D,2014::A,C$1,2014::C,A8)
    B9  =SUMIFS(2014::D,2014::A,C$1,2014::C,A9)
    C1  January
    C2 
    C3 
    C4 
    C5 
    C6 
    C7 
    C8 
    C9 
    Notes.
    Formula in January::B2 can be filled down across B2:B9.
    The target month name is defined in January::C1.
    February table is the same as January table except for the value in C1.
    And the following sample tables are using date per se instead of month name to filter the data. In this scheme, you don't need month column in source table but the retrieving formulae in destination table become more complex.
    2014 (excerpt)
    A1  date
    A2  2013-01-15
    A3  2013-01-20
    A4  2013-01-27
    B1  category
    B2  A
    B3  B
    B4  C
    C1  amount
    C2  100
    C3  50
    C4  20
    January
    A1  category
    A2  A
    A3  B
    A4  C
    A5  D
    A6  E
    A7  F
    A8  G
    A9  H
    B1  totals
    B2  =SUMIFS(2014::C,2014::B,A2,2014::A,">="&EOMONTH(C$1,-1)+1,2014::A,"<="&EOMONTH(C$1,0))
    B3  =SUMIFS(2014::C,2014::B,A3,2014::A,">="&EOMONTH(C$1,-1)+1,2014::A,"<="&EOMONTH(C$1,0))
    B4  =SUMIFS(2014::C,2014::B,A4,2014::A,">="&EOMONTH(C$1,-1)+1,2014::A,"<="&EOMONTH(C$1,0))
    B5  =SUMIFS(2014::C,2014::B,A5,2014::A,">="&EOMONTH(C$1,-1)+1,2014::A,"<="&EOMONTH(C$1,0))
    B6  =SUMIFS(2014::C,2014::B,A6,2014::A,">="&EOMONTH(C$1,-1)+1,2014::A,"<="&EOMONTH(C$1,0))
    B7  =SUMIFS(2014::C,2014::B,A7,2014::A,">="&EOMONTH(C$1,-1)+1,2014::A,"<="&EOMONTH(C$1,0))
    B8  =SUMIFS(2014::C,2014::B,A8,2014::A,">="&EOMONTH(C$1,-1)+1,2014::A,"<="&EOMONTH(C$1,0))
    B9  =SUMIFS(2014::C,2014::B,A9,2014::A,">="&EOMONTH(C$1,-1)+1,2014::A,"<="&EOMONTH(C$1,0))
    C1  2013-01-01
    C2 
    C3 
    C4 
    C5 
    C6 
    C7 
    C8 
    C9 
    Notes.
    Formula in January::B2 can be filled down across January::B2:B9.
    The target month is defined in January::C1, which can be any date in target month, e.g., 2013-01-01, 2013-01-20, etc. The formulae in B will retrieve data with date in range: 2013-01-01 <= [date] <= 2013-01-31.
    February table is the same as January table except for the value in C1.
    Tables are built in Numbers v2.
    Hope this may help,
    H
    EDIT: Replaced the last table with the correct one. (Formulae in B are correct)

  • Assign Month within a date range (by most days in a given month)

    I have a begin and end date, sample data as such
    select to_date('01-13-12','mm-dd-yy') from_dt,
    to_date('02-23-12','mm-dd-yy') to_dt
    from dual
    union all
    select to_date('03-15-2012','mm-dd-yy') from_dt,
    to_date('04-16-2012','mm-dd-yy') to_dt
    from dual
    union all
    select to_date('05-13-2012','mm-dd-yy') from_dt,
    to_date('07-23-2012','mm-dd-yy') to_dt
    from dual
    How do I assign a month by the most days in a month within that date range? Sometimes the date range might have the exact same amount of days in a month (like 3/15/2012 has 16 days and 4/16/2012 has 16 days). In this case, I want the earlier month (march).
    So from the sample data:
    01/13/2012, 02/23/2012, February
    03/15/2012, 04/16/2012, March
    05/13/2012, 07/23/2012, June
    Thanks
    Edited by: user4422426 on Mar 1, 2012 5:15 PM

    Hi,
    Here's one way:
    WITH     cntr          AS
         SELECT     LEVEL - 1     AS n
         FROM     (
                   SELECT      1 + MAX (to_dt - from_dt)     AS max_day_cnt
                   FROM     table_x
         CONNECT BY     LEVEL     <= max_day_cnt
    ,     got_r_num     AS
         SELECT     x.from_dt, x.to_dt
         ,     TRUNC (x.from_dt + c.n, 'MONTH')     AS month
         ,     count (*)                    AS cnt
         ,     ROW_NUMBER () OVER ( PARTITION BY  from_dt, to_dt
                             ORDER BY        COUNT (*)     DESC
                             ,             TRUNC (x.from_dt + c.n, 'MONTH')
                           )     AS r_num
         FROM       cntr     c
         JOIN       table_x  x  ON  c.n  <= x.to_dt - x.from_dt
         GROUP BY  x.from_dt, x.to_dt
         ,       TRUNC (x.from_dt + c.n, 'MONTH')
    SELECT     from_dt, to_dt
    ,     TO_CHAR (month, 'Mon YYYY')     AS mon
    ,     cnt
    FROM     got_r_num
    WHERE     r_num     = 1
    ;Thanks for posting code to create the same data. Please test your code before you post it: you got the order of arguments to TO_DATE reversed.

  • Get number of month, week and date between 2 dates

    Hi all,
    Is it possible to display number of month, week and days between 2 dates? either by using only SQL query or through PL/SQL...
    Input:
    From date: 01-Oct-2010
    To date: 19-Oct-2010
    I want output as below (Assuming the week starts from Monday to Sunday in oracle).
    01-Oct-2010 -- (Since this is in mid of the week)
    02-Oct-2010 -- (Since this is in mid of the week)
    03-Oct-2010 -- (Since this is in mid of the week)
    40 -- (Oct 4 to Oct 10 falls in 40th week of the year)
    41 -- (Oct 11 to Oct 17 falls in 41th week of the year)
    18-Oct-2010 -- (Since this is in mid of the week)
    19-Oct-2010 -- (Since this is in mid of the week)
    Note: If there is one full month between the given date, then the month number should be displayed.
    After the month, the remaining date comprised with one full week, then the week number of the year should
    be displayed. After displaying the week, the remaining dates should be displayed as it is..
    Appreciate your help..
    Thanks.
    Rajan.

    I suppose if it's just like a calendar type information you want then something like...
    SQL> break on month skip 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' as yr from dual)
      2      ,offset as (select case when to_char(trunc(to_date(yr,'YYYY'),'YYYY'),'IW') in ('52','53') then 1 else 0 end as offset from req
      3  select lpad( Month, 20-(20-length(month))/2 ) month,
      4         '('||week||')' as week, "Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"
      5  from (
      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') in ('52','53') then '1'
    10         else to_char(to_number(to_char(dt,'iw'))+offset) end as week,
    11    max(decode(to_char(dt,'d'),'1',lpad(to_char(dt,'fmdd'),2))) "Mo",
    12    max(decode(to_char(dt,'d'),'2',lpad(to_char(dt,'fmdd'),2))) "Tu",
    13    max(decode(to_char(dt,'d'),'3',lpad(to_char(dt,'fmdd'),2))) "We",
    14    max(decode(to_char(dt,'d'),'4',lpad(to_char(dt,'fmdd'),2))) "Th",
    15    max(decode(to_char(dt,'d'),'5',lpad(to_char(dt,'fmdd'),2))) "Fr",
    16    max(decode(to_char(dt,'d'),'6',lpad(to_char(dt,'fmdd'),2))) "Sa",
    17    max(decode(to_char(dt,'d'),'7',lpad(to_char(dt,'fmdd'),2))) "Su"
    18    from ( select trunc(to_date(req.yr,'YYYY'),'y')-1+rownum dt
    19           from all_objects, 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 YYYY'),     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') in ('52','53') then '1'
    25                                                  else to_char(to_number(to_char(dt,'iw'))+offset) end
    26    ) x
    27  order by to_date( month, 'Month YYYY' ), to_number(x.week)
    28  /
    Enter value for required_year_yyyy: 2010
    old   1: with req as (select '&Required_Year_YYYY' as yr from dual)
    new   1: with req as (select '2010' as yr from dual)
    MONTH                WEEK Mo Tu We Th Fr Sa Su
        January 2010     (1)               1  2  3
                         (2)   4  5  6  7  8  9 10
                         (3)  11 12 13 14 15 16 17
                         (4)  18 19 20 21 22 23 24
                         (5)  25 26 27 28 29 30 31
       February 2010     (6)   1  2  3  4  5  6  7
                         (7)   8  9 10 11 12 13 14
                         (8)  15 16 17 18 19 20 21
                         (9)  22 23 24 25 26 27 28
         March 2010      (10)  1  2  3  4  5  6  7
                         (11)  8  9 10 11 12 13 14
                         (12) 15 16 17 18 19 20 21
                         (13) 22 23 24 25 26 27 28
                         (14) 29 30 31
         April 2010      (14)           1  2  3  4
                         (15)  5  6  7  8  9 10 11
                         (16) 12 13 14 15 16 17 18
                         (17) 19 20 21 22 23 24 25
                         (18) 26 27 28 29 30
          May 2010       (18)                 1  2
                         (19)  3  4  5  6  7  8  9
                         (20) 10 11 12 13 14 15 16
                         (21) 17 18 19 20 21 22 23
                         (22) 24 25 26 27 28 29 30
                         (23) 31
         June 2010       (23)     1  2  3  4  5  6
                         (24)  7  8  9 10 11 12 13
                         (25) 14 15 16 17 18 19 20
                         (26) 21 22 23 24 25 26 27
                         (27) 28 29 30
         July 2010       (27)           1  2  3  4
                         (28)  5  6  7  8  9 10 11
                         (29) 12 13 14 15 16 17 18
                         (30) 19 20 21 22 23 24 25
                         (31) 26 27 28 29 30 31
        August 2010      (31)                    1
                         (32)  2  3  4  5  6  7  8
                         (33)  9 10 11 12 13 14 15
                         (34) 16 17 18 19 20 21 22
                         (35) 23 24 25 26 27 28 29
                         (36) 30 31
       September 2010    (36)        1  2  3  4  5
                         (37)  6  7  8  9 10 11 12
                         (38) 13 14 15 16 17 18 19
                         (39) 20 21 22 23 24 25 26
                         (40) 27 28 29 30
        October 2010     (40)              1  2  3
                         (41)  4  5  6  7  8  9 10
                         (42) 11 12 13 14 15 16 17
                         (43) 18 19 20 21 22 23 24
                         (44) 25 26 27 28 29 30 31
       November 2010     (45)  1  2  3  4  5  6  7
                         (46)  8  9 10 11 12 13 14
                         (47) 15 16 17 18 19 20 21
                         (48) 22 23 24 25 26 27 28
                         (49) 29 30
       December 2010     (49)        1  2  3  4  5
                         (50)  6  7  8  9 10 11 12
                         (51) 13 14 15 16 17 18 19
                         (52) 20 21 22 23 24 25 26
                         (53) 27 28 29 30 31
    61 rows selected.
    SQL>

  • How to derive month/year from date in SAP BW 3.5 data flow

    Hi
    How we can derive cal year/month and fiscal month/year from date in SAP BW 3.5 data flow (we're using transfer and update rule)..
    Thanks,
    PK

    Hi,
    if you have any date filed in source side you can just map to any time char system will automatically convert to target objects.
    please look at the screen shot for understanding. (not 3.x it is 7.x)
    Thanks,
    Phani.

  • Extract Month & Year  From Date

    Hi All,
    I have a key figure quantity and i want the data on date,for the month and for the year.
    i create the variables for the date,for the month(ie from date to date) and for the year(ie, from date to date) seperatly that means i have three input variables which may sometimes create confusion.
    My problem is that I want to enter only date and the variable itself calculate the month and year from the date so that it returns the data as desired above.
    for eg. : Now i to enter date : 3/14/2009
              enter MTD (FROM/TO) : 3/01/2009 - 3/14/2009
              enter YTD (FROM/TO) : 4/01/2008 - 3/14/2009
    I want to enter only date : 3/14/2009
    and the variable itself extract the month and year till date.
    and also the same variable calculates number of days so that need not to take the formula variable of date difference to calculate the average qty.
    Neha..

    Hi,
    1. Create User Entry Variable on 0CALDAY : Name = ZCDAY.
    2. Craete a Customer Exit Variable on 0CALDAY: Name  = ZMTD.
    3. Craete a Customer Exit Variable on 0CALDAY: Name  = YMTD.
    Properties of Customer Exit Variables.
    Variable reporesents = Single
    Variable Entry = Mandatory
    Processing By = Customer Exit.
    Character = Calender Day
    Here I'm thinking that ZKF is your key figure.
    In columns you create two selections one is for MTD and other is for YTD.
    In MTD selection, drag and drop ZKF and Drag and Drop 0CALDAY and then Right Clcik and Restrict-->
    Selection = Value Range. (In Between) and restrict with the follwoing Variables
    []ZMTD; ZCDAY.
    In YTD selection, drag and drop ZKF and Drag and Drop 0CALDAY and then Right Clcik and Restrict-->
    Selection = Value Range. (In Between) and restrict with the follwoing Variables
    []YTD; ZCDAY.
    Then write the following code in I_STEP = 2.
    DATA: ZT_DT1 TYPE SY-DATUM,
              ZT_DT2 TYPE SY-DATUM,
              ZT_SDT TYPE SY-DATUM,
              ZT_YR(4) TYPE N,
              ZT_DY(2) TYPE N,
              ZT_MT(2) TYPE N,
              ZE_TT(2) TYPE N,
              ZPOPER TYPE POPER,
             ZRELJR TYPE RELJR.
    WHEN 'ZMTD_A'.
      LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZCDAY'.
        IF sy-subrc = 0.
          CLEAR: l_s_range.
          ZT_DY = '01'.
          ZT_SDT = loc_var_range-low.
          ZT_MT = ZT_SDT+4(2).
          ZT_YR = ZT_SDT+0(4).
          CONCATENATE ZT_YR ZT_MT ZT_DY INTO ZT_DT1.
          l_s_range-low = ZT_DT1.
          l_s_range-sign = 'I'.
          l_s_range-opt = 'EQ'.
          APPEND l_s_range TO e_t_range.
        ENDIF.
      ENDLOOP.
    WHEN 'ZYTD'.
      LOOP AT i_t_var_range INTO loc_var_range WHERE vnam = 'ZCDAY'.
        IF sy-subrc = 0.
          CLEAR: l_s_range.
          ZT_DY = '01'.
          ZT_SDT = loc_var_range-low.
          CALL FUNCTION 'DATE_TO_PERIOD_CONVERT'
            EXPORTING
              I_DATE               = ZT_SDT
    *         I_MONMIT             = 00
              I_PERIV              = 'V3'
           IMPORTING
             E_BUPER              = zbuper
             E_GJAHR              = zbdatj
           EXCEPTIONS
             INPUT_FALSE          = 1
             T009_NOTFOUND        = 2
             T009B_NOTFOUND       = 3
             OTHERS               = 4
          IF SY-SUBRC <> 0.
             MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
          ENDIF.
          CALL FUNCTION 'FIRST_AND_LAST_DAY_IN_YEAR_GET'
            EXPORTING
              I_GJAHR              = zbdatj
              I_PERIV              = 'V3'
           IMPORTING
             E_FIRST_DAY          = ZT_DT2
    *         E_LAST_DAY           =
           EXCEPTIONS
             INPUT_FALSE          = 1
             T009_NOTFOUND        = 2
             T009B_NOTFOUND       = 3
             OTHERS               = 4
          IF SY-SUBRC <> 0.
             MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
             WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
          ENDIF.
          l_s_range-low = ZT_DT2.
          l_s_range-sign = 'I'.
          l_s_range-opt = 'EQ'.
          APPEND l_s_range TO e_t_range.
        ENDIF.
      ENDLOOP.
    Thanks
    Reddy

  • Get the month from a date column with the calculated column

    I am trying to get the month from a date field with the help of calculated column. However I get this syntax error whenever I want to submit the formula:
    Error 
    The formula contains a syntax error or is not supported. 
    the default language of our site is German and [datum, von] is a date field.

    Hi,
    I have created two columns
    Current MM-YY
    Calculated (calculation based on other columns)
    Today
    Date and Time
    Current MM-YY is calculated value with formula as
    =TEXT(Today,"mmmm")
    But the output shows as December instead of May.
    I have tried =TEXT([Datum, von];"mmmm") but no help.
    I am trying to populated the column automatically with current month..ex: if its May the field should show May, next month it should show June an so on.
    Any kind help is grateful.
    Regards,
    Pradeep

  • How to get LASTDAY for each and every month between given dates..

    Hi Friend,
    I have a doubt,How to get LASTDAY for each and every month between given dates..
    for ex:
    My Input will be look like this
    from date = 12-01-2011
    To date = 14-04-2011
    And i need an output like
    31-01-2011
    28-02-2011
    31-03-2011
    is there any way to achieve through sql query in oracle
    Advance thanks for all helping friends

    Here's a 8i solution :
    select add_months(
             trunc(
               to_date('12-01-2011','DD-MM-YYYY')
             ,'MM'
           , rownum ) - 1 as results
    from all_objects
    where rownum <= ( months_between( trunc(to_date('14-04-2011','DD-MM-YYYY'), 'MM'),
                                      trunc(to_date('12-01-2011','DD-MM-YYYY'), 'MM') ) );
    The above two query is worked in oracle 11GActually the first query I posted is not correct.
    It should work better with
    months_between(
       trunc(to_date(:dt_end,'DD-MM-YYYY'),'MM'),
       trunc(to_date(:dt_start,'DD-MM-YYYY'),'MM')
    )Edited by: odie_63 on 12 janv. 2011 13:53
    Edited by: odie_63 on 12 janv. 2011 14:11

  • Display of month insted of date in report!

    Hi Experts,
    Question 1:
    I want to display month instead of date as a characteristic in my report. Is this possible in BEx without using Virtual Characteristic and without changing existing data module.
    Question 2:
    I am able to display the month as keyfigure using formula variable, but it is displaying as 201.106 insted of 201106. How to correct this? means with out displaying the thousand separator for this keyfigure.
    Thanks.
    Herozoh.
    Help will be highly appreciated.

    Hi Herzoh,
    In order to display calday as calmonth.. create a formula variable with replacement path  to get the date into formula.
    While creating in replacement path tab give offset start and length. In the currency unit tab under dimensions select date.
    Note :But the problem is you have to drag 0calday in rows.
    so 0calday also visible in the report.
    I don't know how to hide a charecteristic.
    Regards
    Ranganath.

Maybe you are looking for