Getting 1st of Month Previous To Current Month

Hello, trying to get the first of the month previous to the current month. So if Sysdate is 6/10/20, then I want 5/1/2010 returned for criteria..
No idea how to go about this, any help is appreciated..

Hi
select trunc(add_months(sysdate,-1),'MM') as previous_month from dual;regards,
David.

Similar Messages

  • SQL Query to add previous month amount to current month amount

    Beginner - Using SQL 2008 R2 - Having a difficult time adding previous month amount to current month amount on a continual running basis. Table fields and example data are:
    FunctionID    
    UnitID     
    Dateof           
    Result       YTD
    A                   AA           01/01/2014    10             10
    A                   AA           02/01/2014    10            
    20
    A                   AA           03/01/2014     15           
    35
    B                  BB            01/01/2014     20          
    20
    B                  BB            02/01/2014     10           30
    The YTD field would be a calculated field. I would need to be able start a new YTD in Jan of the next year. Really could use some help and direction on this one. Easy to do in an Excel spreadsheet. Can't seem to get it in SQL. Thanks in advance.

    As of 2008 R2 the choices are:
    - Self join
    - Correlated subquery (in the SELECT or APPLY clauses)
    - Cursor
    - SQLCLR aggregation function (winner)
    The first two options yield a poor performance for tables with considerable number of rows.
    Example:
    select
        A.*, 
        select
            sum(B.Result)
        from
            T as B
        where
            B.FunctionID = A.FunctionID
            and B.UnitID = A.UnitID
            and B.Dateof >= dateadd(year, datediff(year, '19000101', A.Dateof), '19000101') --bofyear
            and B.Dateof <= A.Dateof
        ) as YTD
    from
        T as A;
    Check these old but very interesting posts from Adam Machanic.
    http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/running-sums-redux.aspx
    http://sqlblog.com/blogs/adam_machanic/archive/2006/07/12/running-sums-yet-again-sqlclr-saves-the-day.aspx
    The things get easier from SS 2012 with the enhanced OVER clause (introduction of window frame extent).
    select
        sum(Result) over(
        partition by FunctionID, UnitID, year(Dateof) 
        order by Dateof
        rows between unbounded preceding and current row
        ) as YTD
    from
        T;
    http://sqlmag.com/sql-server-2012/sql-server-2012-how-write-t-sql-window-functions-part-3
    AMB
    Some guidelines for posting questions...
    AYÚDANOS A AYUDARTE, guía básica de consejos para formular preguntas

  • How  to  get the FIRST DAY OF THE CURRENT MONTH

    how to get the FIRST DAY OF THE CURRENT MONTH in oracle 9i.
    plzzzzz send immedaily.advance thanks

    TEST@test SQL> select trunc(sysdate,'MON') from dual;
    TRUNC(SYS
    01-OCT-06
    TEST@test SQL>                                  

  • Last/latest 10 months data ( including current month)

    Hi,  
    I need to find the last/latest 10 months data ( including current month)
    Year
    1/1/2014
    1/2/2014
    1/3/2014
    1/4/2014
    1/5/2014
    1/6/2014
    1/7/2014
    1/8/2014
    1/9/2014
    1/10/2014
    1/11/2014
    1/12/2014
    1/1/2013
    1/2/2013
    1/3/2012
    1/4/2012

    Hi,
    this is my data 
    Year column with Nvarchar datatype. 
    Year
    1/1/2014
    1/2/2014
    1/3/2014
    1/4/2014
    1/5/2014
    1/6/2014
    1/7/2014
    1/8/2014
    1/9/2014
    1/10/2014
    1/11/2014
    1/12/2014
    1/1/2013
    1/2/2013
    1/3/2012
    1/4/2012
    now form this yea) column i need to fetch the latest/last 10 months data only .
    OutpUt for the Query to be:
    Year
    1/3/2014
    1/4/2014
    1/5/2014
    1/6/2014
    1/7/2014
    1/8/2014
    1/9/2014
    1/10/2014
    1/11/2014
    1/12/2014
    Hope this will give you clear picture

  • Is there any standard variable to calculate previous and current month

    hi,
    Is there any standar variable to calculate previous month and current month which will calcualte from system date (sy-date). I found standard SAP Delivered variable - 0CMONTH to calculate current month. But i want both current month and previous month.  IF it is not avialble can you please provide me the ABAP code..
    thanks in advance.

    Hi,
    Check this thread.
    ABAP routine for loading previous months data in infopackage
    Regards.

  • How to take last months date to current month date

    Hi,
    I am designing a report wherein the data should range from 12:01am on the 28th day of the previous month to 11:59 pm on the 27th of the current month..  Also this report would be scheduled accordingly and I should  not be able to modify the report every month.  What is the formula that should go into it.
    Any help will be greatly appreciated!
    Thanks & Regards
    Cauvery

    Hi Cauvery,
    try to use the following formula :
    Local DateTimeVar A;
    Local DateTimeVar From_;
    Local DateTimeVar To__;
    A:=today;
    if Month(A)=1
    then From_:=Datetime(year(A),12,28,0,1,0)
    else From_:=Datetime(year(A),Month(A)-1,28,0,1,0);
    To__:=Datetime(year(A),Month(A),27,23,59,0);
    Regards
    Edited by: jeecech on Apr 17, 2009 9:37 AM

  • Display three prior months starting from current month

    Hello All,
    I came across the requiremnt:
    The report looks like:
    2014-Jan
    2014-Feb
    2014-Mar
    2014-Apr
    2014-May
    2014-June
    2014-July
    2014-Aug
    2014-Sep
    2014-oct
    2014-Nov
    2014-Dec
    A
    122
    233
    233
    233
    233
    233
    233
    233
    233
    233
    233
    233
    B
    22
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    C
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    33
    D
    333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    The requirement is to display the prior 3 months ,current month, next 3 months.
    Assume that current month is SEPTEMBER, the report should look like:
    2014-June
    2014-July
    2014-Aug
    2014-Sep
    2014-oct
    2014-Nov
    2014-Dec
    A
    233
    233
    233
    233
    233
    233
    233
    B
    33
    33
    33
    33
    33
    33
    33
    C
    33
    33
    33
    33
    33
    33
    33
    D
    3333
    3333
    3333
    3333
    3333
    3333
    3333
    Share your thoughts.
    Thanks

    Hello All,
    I got this resolved using the below formula to get either prior 3 months or the next 3 months from current date:
    3rd month from current date:
    =LastDayOfMonth(RelativeDate(LastDayOfMonth(RelativeDate(LastDayOfMonth(RelativeDate(LastDayOfMonth(CurrentDate()); 1));1));1))
    Prior 3rd month from current date:
    =ToDate(FormatDate(RelativeDate(ToDate(FormatDate(RelativeDate(ToDate(FormatDate(RelativeDate(ToDate(FormatDate(CurrentDate();"MM/yyyy");"MM/yyyy");-1);"MM/yyyy");"MM/yyyy");-1);"MM/yyyy");"MM/yyyy");-1);"MM/yyyy");"MM/yyyy")
    and used these objects to restrict my date field(date object between "Prior 3rd month from current date:" and
    "Prior 3rd month from current date:"
    Thanks,
    Vijay

  • How to get previous month data from current month values

    Hi Experts,
    I have made one universe from BW Query in which Fiscal year period is entered in interval.
    I have made a universe from that and want to develop webI reports on top of that.
    In my webI reports, i have used one cross tab. In Rows section i have added Company Code and in Column section i have used Fiscal Year/Period and in Value section i have added Sales Value. I want this value of previous month.
    Requirement:
    Ex.
                            Feb'09          Mar'09     and so on...
    Comp_code1   Sales of Jan'09         Sales of Feb'0f         and so on....
    I am getting this.
    Ex.
                            Feb'09          Mar'09     and so on...
    Comp_code1   Sales of Feb'09         Sales of Mar'09         and so on....
    I hope i have clear my requirements.
    Please help as soon as possible.
    Thanks in Advance,
    Rishit

    Hi Rishit,
    Follow the below steps to get the desired result.
    Step1: Convert your fiscal year period from char to a date in your database or in your designer however its feasible.
    to_date('substr('009.2009',2)','mm.yyyy')
    you will get the result 01 sep 2009
    Step2: Convert this format to 01/09/2009 by using date functions.
    Step3: Create a Detail associated to the 'date' field (typically your fiscal period).
    Step4: Create a cross tab Like : Rows section should have Company Code and in Column section should have 'date'(created detail) and in Value section should be Sales Value.
    you should get the following result.
    01/02/2009 01/03/2009 and so on...
    Comp_code1 Sales of Feb'09 Sales of Mar'09 and so on....
    Step5: Use the following formula in your Column (date) formula bar.
    =(<date>-1)-DayNumberOfMonth(<date>-1)+1
    You will get the following result:
    01/01/2009 01/02/2009 and so on...
    Comp_code1 Sales of Feb'09 Sales of Mar'09 and so on....
    Format the cell according to your reruirement.
    Let me know if you will get any break in the above steps.
    Regards,
    Swati.

  • Function to get the last day of the current month

    Hi friends
    Now I need to know a function to get the last day of a month. I had between my notes that function but I do not find it and I think you can give the answer faster.
    Thanks
    Joel Pérez
    DBA Oracle

    I know emoticons are a bit naff but in the absence of a UBB markup to indicate humourous intent they do serve to indicate a joke. This is useful in a purely verbal domain like these forums, especially given that many participants don't have English as their first lanaguage and so often miss the wordplay.
    Cheers, APC

  • Calendar to get week number of current month

    Hi All,
    Description: Currently I am working on an application which require to calculate some data for current month from a database where I have the data of all the previous and current month of that year, I can take week number as criteria.
    Help Required: I can find week of month or year using Calendar methods but how to find the first week on the current month only.
    Lets suppose the current month is April so when I use, int weekofmonth = cal.get(cal.WEEK_OF_MONTH);
    it will return me the number of present week however I want to get what would be the week number at the start of the month (April).
    Hope I am able to describe my query properly.
    Thanks in advance.
    Amit

    / ====================================================
    Method: Get the desired Date format for the date
    Developed By: Sandip Waghole [29-Jan-2010]
    ==================================================== /
    public String getWeekNo(String strDate)
    // input Date Format : M/dd/yyyy
    int weekNo=0,i=0;
    String strWeekNo=null;
    int noOfDaysInTheYear=365;
    int WEEK_STARTS_ON = 1; // Define the day on which week starts Sunday/Monday 1:Sunday 2:Monday
    int firstDayNoInFirstWeekOfPresentYear=0; // Inititalize teh day on which week is starting in present year
    int firstDayOfPresentYear=0; // Inititlize the 1st day of the present year whether Sunday/Monday/.....
    int[] monthDaysArray = {31,28,31,30,31,30,31,31,30,31,30,31}; // Define array of the days as per months
    int todaysDayNoInPresentYear=0;
    int daysLateByFirstWeekStartedAfterYearStarted=0;
    int intTemp=0;
    //strDate="08/24/2000"; // For test purpose
    StringTokenizer strDateTok = new StringTokenizer(strDate, "/ ");
    int month = Integer.parseInt(strDateTok.nextToken());
    int day= Integer.parseInt(strDateTok.nextToken());
    int year = Integer.parseInt(strDateTok.nextToken());
    GregorianCalendar cal = new GregorianCalendar();
    // Check if present year is leap year
    boolean boolIsLeapYear = cal.isLeapYear(year);
    // If it is boolean year then add 1 to total days in the year & add one more day to february
    if(boolIsLeapYear)
    noOfDaysInTheYear=noOfDaysInTheYear+1;
    monthDaysArray[1]=monthDaysArray[1]1;
    // Find the 1st day of this year
    Calendar calObj = new GregorianCalendar(year, Calendar.JANUARY, 1);
    firstDayOfPresentYear = calObj.get(Calendar.DAY_OF_WEEK);
    int intRemoveNoOfDaysFromWeek=0;
    // # Find the day no of prsent day
    for(i=0;i<month;i+) // get no of days till present year
    intTemp = intTemp monthDaysArray;
    todaysDayNoInPresentYear = intTemp - (monthDaysArray[month-1]-day);
    if(firstDayOfPresentYear==6 || firstDayOfPresentYear==7) // If first Day is Friday or Saturday then it is week
    // Identify the the day no on which 1st week of present year is starting
    firstDayNoInFirstWeekOfPresentYear = 7 - firstDayOfPresentYear WEEK_STARTS_ON 1;
    // Find delay in the 1st week start after r=the year start
    daysLateByFirstWeekStartedAfterYearStarted = firstDayNoInFirstWeekOfPresentYear - 1;
    // Now week is starting from Sunday
    weekNo = (Integer)((todaysDayNoInPresentYear-daysLateByFirstWeekStartedAfterYearStarted)/7);
    // Find the day no of today
    intTemp = (todaysDayNoInPresentYear-daysLateByFirstWeekStartedAfterYearStarted) % 7;
    if(intTemp > 0)
    weekNo=weekNo+1;
    else
    weekNo=weekNo;
    else
    // 1st week is starting on 1st Of January
    firstDayNoInFirstWeekOfPresentYear=firstDayOfPresentYear;
    // Remove no. of days from the 1st week as week is starting from odd Sunday/Monday/Tuesday/Wednesday/Thursday
    intRemoveNoOfDaysFromWeek = 7-firstDayOfPresentYear 1; // 1 added as include start day also
    // So one week will be added in no. of weeks
    weekNo = (Integer)((todaysDayNoInPresentYear-intRemoveNoOfDaysFromWeek)/7);
    // Find the day no of today
    intTemp = (todaysDayNoInPresentYear-intRemoveNoOfDaysFromWeek) % 7;
    weekNo = weekNo +1; // As 1st weeks days are reduced from the todays day no in the year
    if(intTemp > 0)
    weekNo=weekNo+1;
    else
    weekNo=weekNo;
    // Remove the no. of days from the week 1
    strWeekNo=Integer.toString(weekNo);
    return strWeekNo;
    // Any issues please mail on [email protected] or [email protected]

  • Current month and Previous Month

     Hi, I have the table shown above. I m trying to get output something like I need current month total amount and the total of the previous month. Can someone please help on this?  THank you
    ID           PreviousMonthTotal        CurrentMOnthTotal      
    1              3000                                3000                   

    Please say your @@version,
    on
        Microsoft SQL Server 2012 (SP1) - 11.0.3412.0 (X64)
        Mar  2 2014 01:25:09
        Copyright (c) Microsoft Corporation
        Standard Edition (64-bit) on Windows NT 6.2 <X64> (Build 9200: )
    query show ZEROS.
    DECLARE  @TEMP as TABLE
        id int,date_t datetime,amount int
    INSERT @TEMP (id,date_t,amount)
    VALUES   (1,'20150112',1000)
            ,(1,'20150212',1000)
            ,(1,'20150122',2000)
            ,(1,'20150202',200)
            ,(1,'20160112',41000)
            ,(2,'20150112',1001)
            ,(2,'20150212',1003)
            ,(2,'20150122',2004)
            ,(2,'20150202',205)
            ,(2,'20160112',41020)
            ,(2,'20160212',11200)
            ,(2,'20140122',201)
            ,(2,'20140202',20)
    SELECT ID as [Cust Ref]
    , SUM(CASE WHEN Date_t = dateadd(MM,-1,getdate()) THEN Amount ELSE 0 END) as [Total LAST Month]
    , SUM(CASE WHEN Date_t = getdate()  THEN Amount ELSE 0 END) as [Total This Month]
    FROM @temp
    GROUP BY ID

  • Get Week Number of Current Month

    How can I get the week number of the current month (1-5)?
    This is what I have tried and I'm not getting the expected
    results.

    quote:
    Originally posted by:
    DJ5MD
    Does your go 1-5 since it's only 30 days? Is that the
    difference?
    I think he was referring to your comment "How can I get the
    week number of the current month (1-5)?". Some months can have 6
    weeks so you should take that into account.
    Both the function from cflib.org and his code should return 6
    for 2007-09-30.

  • To show previous 12 months from current month.

    I have one column Time.Month (Char column) with values in the format 2011 / 09. In my report i want to show previous 12 months from the current month. I dont have any date prompts in my report. Pls help.

    Use the below query in init block to create a repository variable as PREVIOUS_12_MONTH
    SELECT PRV.PER_NAME_MONTH FROM VALUEOF(OLAPTBO).W_DAY_D PRV , VALUEOF(OLAPTBO).W_DAY_D CUR WHERE convert(char(12),PRV.DAY_DT)=convert(char(12),CUR.YEAR_AGO_DT) AND convert(char(12),CUR.DAY_DT)=convert(char(12),getdate()-valueof(time_offset))
    Variable PREVIOUS_MONTH: Use the below query in init block
    SELECT PRV.PER_NAME_MONTH FROM VALUEOF(OLAPTBO).W_DAY_D PRV , VALUEOF(OLAPTBO).W_DAY_D CUR WHERE convert(char(12),PRV.DAY_DT)=convert(char(12),CUR.MONTH_AGO_DT) AND convert(char(12),CUR.DAY_DT)=convert(char(12),getdate()-valueof(time_offset))
    Create a filter in Answers as below and use where ever you wish:
    Month is between PREVIOUS_12_MONTH and PREVIOUS_MONTH
    Use it in the report with current month Or previous month as per your requirements.
    Pls mark if helps

  • Displaying current month with all empty months

    None of the answers I've seen for showing current month in a calendar has given a solid answer. So, here is my question. My objective is to display all calendar months rather they are empty or not, plus I want the calendar to start at the current month. I added coding to display a next month and previous month button, so you should be able to click next or previous month from the current month showing. I have the "Show All Empty Months" checked in the Display Options under Common Options and the start and end date are empty. I have a bind variable in my coding that defaults to the current month but it will not show an empty month. I get the "no rows returned" situation because it doesn't have anything in it. Is there a solution to showing all empty months?
    Also, has anyone found a solution to hide/remove the default Next/Previous buttons? I read in one of the replies that this would be fixed in a forthcoming release. Is this true?
    Thanks for any assistance.

    The calendar works on a sql query result. I had done something similar to what you are looking for by spoofing the sql query to return rows for all the months which I am interested in. I had a query like the following.
    select *
    from scott.emp a , ( select (trunc(sysdate) - 7910 + rownum) dat from user_objects ) b
    where b.dat = a.hiredate (+)
    Hope you can do something similar to this.

  • Running Sum on column total minus current month

    Hi all,
    Is it possible to get the running sum total value of all the months minus the current month value in the column function,as I need to display the same in the narrative view.
    Like RSUM(YTD(M-1)Value)...
    Presently this RSUM is not giving me the right value.
    I can also do the total for all the months minus the current month value.Can it be done at the column funtion?
    Regards
    Ashish

    Hi
    Can you elaborate a little..Did you mean I need to create one variable in the repository?
    YTD (M-1 Value) / (YTD Last Year M 0 Value)
    where M0 is current month and M-1 is previous month and value is a measure.
    Regards
    Ashish
    Edited by: Ashish21473 on Jan 7, 2011 3:38 AM

Maybe you are looking for