Function to find the Last Date of Month One Year Ago - RETURNS ERROR

I've written sql code which takes a date and finds the Last Day of the Month one year ago. For example,  it takes the date '2015-04-17' and returns the date '2014-04-30'. The code works fine in a query. Now I'm trying to turn this into a function. However,
when I try to create the function I get the error:
Operand type clash: date is incompatible with int
Why is this error being returned?
Here is my function:
CREATE FUNCTION dbo.zEOM_LY_D(@Input Date)
       RETURNS date
AS
BEGIN;
  DECLARE @Result date;
  SET @Result =  convert(DATE, DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,dateadd(m, -11, @Input)+1),0)),101)
    RETURN @Result;
END;
Thanks for any help you can give.
                 

Stan,
Thanks very much- that does the trick. I should have said I am using SQL 2008 so EOMONTH is not available to me. I still don't get why I got an error though, since I was treating a date like a date and not like an int.
Thanks, John
I think i found the issue but i do not know why it is causing the issue. i think may be the way dates are treated/stored internally..i think dates are stored as no of days after 0001/01/01 but cannot see how this cwould effect..may be somebody else can through
some light..... 
if you make it as datetime it will work,
if you leave it as date - eliminate adding 1...  you should prefer to use dateadd to add/substract dates.
try this to understand..
--removed the +1 in the code
declare @input date
set @input ='20150503'
select
convert(DATE, DATEADD(s,-1,DATEADD(mm,DATEDIFF(m,0,dateadd(m, -11, @Input)),0)),101)
go
--made the datatype as datetime
declare @input datetime
set @input ='20150503'
select
convert(DATE, DATEADD(s,-1,DATEADD(mm,DATEDIFF(m,0,dateadd(m, -11, @Input)+1),0)),101)
Hope it Helps!!

Similar Messages

  • Fn module to find the last date of month

    Hi,
    Is there any fn module to check that particular date is last of the montn?
    Regards,
    Sonika

    Hi,
    Take a look at :
    RP_LAST_DAY_OF_MONTHS
    Hope this helps,
    Erwan

  • I wish I could remember what exactly happenned the last time I upgraded iphoto years ago, but something

    I wish I could remember what exactly happenned the last time I upgraded iPhoto years ago. Did my address book go kaflooey? But whateverit was, it discouraged me from any updating eversince.  Believe it or not, I have been limping along with Iphoto 6.0.6 and basically faring fine, given my needs.  But I would now like to be able to order prints from my photo library -- and you can't do that any longer with 6.  So my question is:  what are the things that
    can go wrong with the later systems?  And is there any way around having that happen?

    Make a back up.
    Make a second.
    Upgrade. Get on with your life.
    Regards
    TD

  • First day of current month, one year ago

    All,
    Does anyone have a calculation or know how I can calculate the first day of the current month, one year ago? I am trying to setup a filter criteria to show all records created >= 1st day of current month for prior year and <= the last day of the prior month. I have the second half of the equation for last day prior month but need some help on the first half.
    Thanks in advance for any pointers!
    D

    You can try:
    SELECT TRUNC(ADD_MONTHS(sysdate, -12), 'MON') FROM dual;
    Or
    SELECT TRUNC(sysdate-NUMTOYMINTERVAL(1,'YEAR'), 'MON') FROM dual;

  • How ti find the last date of execution of a bookmark on the portal BW?

    Hello,
    We need to know the last date of execution of a bookmark on the portal BW?
    Thanks for your help.
    Best regards

    Hi,
    Please check the below URL for Managing bookmark:
    http://help.sap.com/saphelp_nw04/helpdata/en/2d/ce623c1bb4646ce10000000a11402f/frameset.htm
    http://help.sap.com/saphelp_nw04/helpdata/en/3a/d609e5803111d5992f00508b6b8b11/frameset.htm
    Thanks,
    Venkat

  • Is there anyway I can find the LAST date of an app downloaded more than once?

    I look at my recent purchases and it shows the FIRST time i purchased the free app, however it doesn't show any other time that it's been downloaded and I know I've downloaded it 4-5 more times. Is there ANY way I can find ALL of the dates that I re-downloaded this app?

    Change your iTunes (Apple ID) password along with any other password that was stored in the iPod.  If any passwords are associated with credit cards, contact the CC company and get your card replaced (with a new number).  If any passwords are associated with your bank or any savings institution, contact them also and discuss approprate action with them.
    The "Find my..." function is pretty much useless if the device is in the hands of a thief.  All that is necessary is for the thief to connect to any computer with iTunes and "Restore as new."
    The only real protection you have is with the personal information on the device rather than the physical device itself.  This requires action before the device is lost/stolen.  If the device has significant personal information, it should have a strong 8-digit (or longer) password AND be configured for automatic wipe in the event of ten consecutive incorrect password entries.

  • IDCS4 - [JS] - How to find the Last paragraph with only One word - Reg.

    Dear All
      Here I have a big doubt!.
          I checked Total number paragraphs in the Text Frame and Each paragraph having only 1 words and then immediatly checked with words. Its only having less than 6 characters or not, Like that I checked and throw the log file.
    But I can't add this content in the file....
    //================= Here my coding ===========================//
             var myDoc = app.activeDocument;
             var myPages = myDoc.pages;
             var myTextFrame;
             var myParagraph;
             var myParaLine;
             var myParaWord;
             var Pagecount= 1 ;
             var paraCount = 1;
             var myChars;
             log1 = myDoc.name.split(".indd")[0];
                FilePath = myDoc.filePath;
               aFile = File(FilePath + "/" + log1 + "_LastLineWords"+ ".log");
               var today = new Date();
               aFile.open("w");
               aFile.seek(0,2);
              aFile.write("\n"+"  Last Line only 2 words logs       "+"\n");
              aFile.write("         - - - - - - - - - - - - - - - -               "+"\n\n");
              aFile.write("\n"+String(today+"\n"));
              aFile.close(); 
             for(var i=0; i<myPages.length; i++)
              myTextFrame = myPages[i].textFrames;
               for(var j=0; j<myTextFrame.length; j++)
                  myParagraph = myTextFrame[j].paragraphs;
                 for(var k=0; k<myParagraph.length; k++)
                    if(myParagraph.length >= 2)
                    myParaLine = myParagraph[k].lines;
                  for(var m=0; m<myParaLine.length; m++)
                     if(myParaLine[m].words.length < 2 )
                       myChars = myParaLine[m].words;
                     for(var n=0; n<myChars.length; n++)
                       if (myChars[n].characters.length < 6 )
                        aFile.open("e");
                        aFile.seek(0,2);
                        app.select(myChars[n])
                        var mySel = app.selection[0];
                        $.writeln(mySel.contents);
                        $.writeln(mySel);
                        aFile.write("\n"+"Contents :"+"\t"+mySel.contents+"\n");
                        aFile.write("\n"+"Line No. of the Content  :"+"\t"+ myParaLine.length+"\n");
                        aFile.write("\n"+"Pargraph No. of the Content  :"+"\t"+ paraCount+"\n");
                        aFile.write("\n"+"Page No. of the Content   :"+"\t"+ Pagecount+"\n");
                        aFile.close();
                   paraCount +=1;
               Pagecount +=1;
       //======================================== Execute the Log File =================================//
                if(aFile.length != 0)
                aFile.execute();
                 else
                 var myDelFile = File(FilePath + "/" + log1 + "_LastLineWords"+ ".log");
                 myDelFile.remove();
                   exit(0);
    //========================== End of the Script =======================================================//
    The $.writeln(mySel); throws [Object Text]... How to write the contents in my Log file.
    This is working in non-xml contents but this is not working in xml contents. Please anybody can check the above code and please give me the suggestion....
    Please any one can help me, I will appreciate....
    Thanks & Regards
    T.R.Harihara Sudhan 

    Welcome to the Apple discussions.
    These discussions are user-to-user. Apple generally doesn't monitor the discussions.
    My Turkish language skills are non-existant, however, you can use this to find an Apple store close to you: http://www.apple.com/uk/buy/locator/ (you choose your country in the locator) . It may be as easy as returning it to a local store. In some countries you have a short time period to return anything bought from Apple (for example, 14 days in the US). If that doesn't work, you want to contact Apple customer service. Unfortunately, this is where I was unable to maneuver through the Turkish Apple site, due to not knowing the Turkish language.

  • I can't find the music i have down loaded year ago before my Iphone crashed!! how can i find them and put them back in my iphone?

    I have downloaded music in the past. Then something happend to my iphone. I lost all those tracks!! Now i try to find them and download them a gain but the Itunes store tell me taht i have downloaded them allready! How can i put them back in my iphone or itunes???

    Start iTunes, under STORE > iTunes Store. Click Purchased (NEW) top rght.
    Note: Music is not supported in all countries. If your country is supported you can click Music, Apps and Books to re-download.

  • Customer Exit for Number of Days from 1 st Apr to last date of Month Enter

    Hello BI Experts,
    I have a requirement to count the number of days from 1 st April of current year to the last date of month entered.
    For example : The use will enter say July 2010 or 003.2010  (as Fiscal Year Variant is V3 ).
    Today is 14 July ...So we have to first find out the end date of the July month ie 31 st July
    Then go to 1 st April 2010.
    Now calculate the Number of days between 1 st April to 31 st July 2010.
    I consider I have to create two Customer Exit variable
    as below
    1 st customer exit Bex variable  say  ZLY_MTH  ( Last day of Month Entered)
      and i_step = 1
    2 nd Customer Exit BEx Formula variable say ZF_NUMDAYS ( Number of days between two dates)
    i_step =1 .
    Please provide me the logic for the above two.
    Thanks in Advance.
    Regards,
    Amol Kulkarni

    PSUDEO CODE:
    1. Initially LOOP AT I_T_VAR_RANGE INTO LOC_VAR_RANGE WHERE VNAM = 'ZMONTH'.
    2. Get the Month input using VAR_MONTH2 = LOC_VAR_RANGE-LOW+4(2)
    3. Now calculate Month+1: VAR_MONTH2 = VAR_MONTH2 + 1 (Refer **)
    4. Now calculate the Current Year: VAR_YEAR = LOC_VAR_RANGE-LOW+0(4).
    5. Get the 1st Day of the Month (VAR_MONTH2):  CONCATENATE '01' '/' VAR_MONTH2 '/' VAR_YEAR INTO L_S_RANGE-LOW.
    6. SUBRACT 1 (0DATE) from this DATE (This will give the logic for last day of the current month)
    Insert this code also for using the date conversions
            CALL FUNCTION 'CONVERSION_EXIT_ALPHA_INPUT'
              EXPORTING
                INPUT  = VAR_MONTH2
              IMPORTING
                OUTPUT = VAR_MONTH2.
    Pls. check out this logic. Guess it would solve your need.
    Thanks,
    Arun Bala

  • Get first and last date of month

    Hi,
      Is there any function module to get first and last date of month for a entered date.
    Please let me know.
    Regards,
    SP

    Hi,
    Use the below FM to find the Last day of month and them u can easily calculate the first day.
    DATA : v_startdate TYPE sy-datum.
    DATA : v_enddate TYPE sy-datum.
    DATA : v_temp TYPE dats.
    v_temp = sy-datum.
    CALL FUNCTION 'RP_LAST_DAY_OF_MONTHS'
        EXPORTING
          day_in            = v_temp
        IMPORTING
          last_day_of_month = v_enddate
    CONCATENATE  v_enddate+0(6)  '01'  INTO  v_startdate.
    Hope it helps.
    Regards,
    Arnab.

  • First Date of the Given date and month

    Hi Evryone,
    I want to extract first day of the given date.
    For Ex: i aam giving date "30-jan-2013" but i want to show "01-jan-2013".
    In Oracle, Trunc function behaving mentioned above. So i need appropriote function in HANA to get the FIRSTDATE....
    I observed HANA Given last_date function but what about First_Date function? can i use it?
    Regards,
    Chandrakanth

    Hi Chandrakanth,
    Did you have a look on this blog:
    SAP HANA: Get the First Date and the Last Date of Month from a Given Date | WuaWua
    There is LAST_DAY in HANA but not FIRST_DAY.
    Since last day of the month may vary but first day of the month will always be '01'. Hence they may not have added that function.
    So either you can follow that approach in the blog or you can simply replace the day '01'.
    Regards,
    Krishna Tangudu

  • Help find the Last run date from previous month

    Hi all
    I am stuck trying to find the Last Run Date from the previous month.
    select distinct(date_ran) from TABLE X where date_ran like '%/11-%' order by date_ran desc gives
    "03/30/11-06:19
    "03/25/11-03:01
    "03/24/11-03:00
    "03/23/11-03:00
    "03/22/11-03:00
    "03/21/11-03:00
    "03/18/11-03:00
    "03/17/11-00:00
    "03/16/11-06:31
    "02/15/11-07:42
    "02/15/11-06:00
    "02/14/11-08:19
    Here the result I am trying to acheive is 02/15/11-07:42 ;Note the column date_ran is a VARCHAR2(255) and not a TIMESTAMP
    I tried
    select ADD_MONTHS(to_date(max(date_ran), 'MM/DD/YYYY-HH24:MI'),-1)
    from daily_tests_a
    where date_ran like '%/11-%'
    order by date_ran desc
    which returns 2/28/0011 6:19:00 AM and that is not the result I am looking for
    Regards
    SMK

    Welcome to the forum!
    user2931503 wrote:
    Hi all
    I am stuck trying to find the Last Run Date from the previous month.
    select distinct(date_ran) from TABLE X where date_ran like '%/11-%' order by date_ran desc gives
    "03/30/11-06:19
    "03/25/11-03:01
    "03/24/11-03:00
    "03/23/11-03:00
    "03/22/11-03:00
    "03/21/11-03:00
    "03/18/11-03:00
    "03/17/11-00:00
    "03/16/11-06:31
    "02/15/11-07:42
    "02/15/11-06:00
    "02/14/11-08:19
    Here the result I am trying to acheive is 02/15/11-07:42 ;Note the column date_ran is a VARCHAR2(255) and not a TIMESTAMPThat's a very bad idea. Points in time should always be stored in DATE (or maybe TIMESTAMP) columns. If not, you're going to waste resources converting them to DATEs whenever you need them to behave like DATEs, and risk run-time errors because of bad data.
    Normally, the "previous" month means the month before the current month. In April, the previous month is March.
    By "previous" month, do you mean the 2nd latest month in the table? That is, this data contains data from February and March, 2011; March 2011 is the latest of those months, February 2011 is the next-to-last, and so is that why you want a date from February?
    I tried
    select ADD_MONTHS(to_date(max(date_ran), 'MM/DD/YYYY-HH24:MI'),-1)
    from daily_tests_a
    where date_ran like '%/11-%'
    order by date_ran desc
    which returns 2/28/0011 6:19:00 AM and that is not the result I am looking for
    Regards
    SMKTry this:
    WITH   got_real_date   AS
         SELECT     TO_DATE ( SUBSTR (date_ran, 1, 14)
                   , 'MM/DD/RR-HH24:MI'
                   )     AS real_date
         FROM     table_x
    --     WHERE     ...     -- If you need any filtering, put it here
    SELECT     TO_CHAR ( MAX (real_date)
              , 'MM/DD/RR-HH24:MI'
              )     AS last_date
    FROM    got_month_num
    WHERE     real_date     < (
                               SELECT  TRUNC (MAX (real_date), 'MONTH')
                         FROM    got_real_date
    ;Edited by: Frank Kulash on Apr 5, 2011 3:59 PM

  • FM to find the first & last date of month when we enter period and year

    Hi Gurus,
               I required an urgent need.
               I want a function module which gives me first and the last date of the month when I enter the period and the year.
               Reply me as soon as possible.
    Regards,
    Sagar

    Try this coding...  enter    02/2007 into the selection screen field and execute
    report zrich_0001 .
    data: start_date type sy-datum,
          end_date type sy-datum.
    parameters: p_spbup type spbup.
    start-of-selection.
      start_date = p_spbup.
      start_date+6(2) = '01'.
      call function 'LAST_DAY_OF_MONTHS'
           exporting
                day_in            = start_date
           importing
                last_day_of_month = end_date.
      write:/ start_date, end_date.
    Regards,
    Rich Heilman

  • How to find FIRST and the LAST date of the month.

    Hello,
    I want to find the first and the last date of the current month through query. How is it possible please help.
    For example if the current month is july. The first date should be 01-JUL-2006 and the last date would be 31-JUL-2006.
    Please help me.
    Regards,
    Imran Baig

    Like this?
    SQL> select trunc(sysdate,'MM') "First_Day",
      2        last_day(sysdate) "Last_Day" from dual;
    First_Day Last_Day
    01-JUL-06 31-JUL-06
    SQL> select trunc(to_date('10-FEB-04'),'MM' ) "First_Day",
      2       last_day(to_date('10-FEB-04')) "Last_Day" from dual;
    First_Day Last_Day
    01-FEB-04 29-FEB-04

  • How to find last date of month

    Dear  All,
    Can anybody tell me how to find last date of month I need it to check some validations
    helpful answer will definatly rewarded
    Regards
    Shashikant

    Hi,
    RP_LAST_DAY_OF_MONTHS Determine last day of month
    HR-D: Payroll Germany code
    RP_LAST_DAY_OF_MONTHS
    HR-D: Determine last day of month
    DATE_CONVERT_TO_FACTORYDATE
    Returns factory calendar date for a date Calculates and returns factory calendar date for a date (if CorrectOption = '+');
    Checks if the date is work day (if CorrectOption = '-').
    HOLIDAY_CHECK_AND_GET_INFO Useful for determining whether or not a date is a holiday. Give the function a date, and a holiday calendar, and you can determine if the date is a holiday by checking the parameter HOLIDAY_FOUND.

Maybe you are looking for

  • Activation error for the extractor 0fc_bp_items.

    Here is the error message that i am facing while activating the data source. 0fc_bp_items.

  • Unwanted Blank line appearing in the output

    Hi all, we are trying to print the line items of a Order. There is no issue in first page. But in second page, after prinitng the first record...a blank line is coming followed by the second record. Now, we need to remove this blank line. There is a

  • Java.lang.NullPointerException was thrown

    Hi, I am doing an inbound interface File XI Idoc(DEBMAS) scenario with a file of fixed length. The File got picked up by the sender file adapter but I am getting a mapping error <i>"java.lang.NullPointerException was thrown"</i>. I disabled the field

  • Unable to send or recieve text messages

    For some reason I am unable to send or recieve text messages, can anyone help me with this situation

  • Sorting a group - help!

    Post Author: bddgp CA Forum: General I've tried the documentation, online help, my Crystal Reports book and the web, and can't find the answer!  My problem:My database contains a list of people and events they have attended.  I have created a report