Substract one day form end date

   
Hi All,
I have requirement where I need subtract one day from end_date if the start date of next row is the same and leaving the last row_id as it is and only if the customer id is the same.
Any suggestions
Current Values:
customer_id row_id start_date  end_date 
100         1      2014-04-01    2014-05-01
100         2      2014-05-01    2014-06-01
100         3      2014-06-01    2014-07-01     
Desired values:
customer_id row_id start_date  end_date 
100         1      2014-04-01    2014-04-30
100         2      2014-05-01    2014-06-30
100         3      2014-06-01    2014-07-01     
-kccrga http://dbatrend.blogspot.com.au/

Hi,
Not sure if the row_id is hard coded or a function, so went with a function. Try this code, it should give you what you need.
DECLARE @Test TABLE(customer_id INT,start_date DATETIME,end_date DATETIME)
INSERT INTO @Test
SELECT 100,'2014-04-01','2014-05-01' UNION
SELECT 100,'2014-05-01','2014-06-01' UNION
SELECT 100,'2014-06-01','2014-07-01' UNION
SELECT 200,'2014-04-01','2014-05-01' UNION
SELECT 200,'2014-05-01','2014-06-01' UNION
SELECT 200,'2014-06-01','2014-07-01'
SELECT customer_id,
start_date,
ROW_NUMBER() OVER (ORDER BY customer_id,start_date) RowNo,
ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY start_date) rowByCust,
end_date,
ISNULL((SELECT TOP 1 DATEADD(DAY,-1,start_date) FROM @Test NextDate WHERE NextDate.customer_id=TestData.customer_id AND NextDate.start_date>TestData.start_date ORDER BY start_date ASC)),end_date) new_end_date
FROM @Test TestData
customer_id
      start_date
RowNo
rowByCust
end_date
new_end_date
100
2014-04-01
1
1
2014-05-01
2014-04-30
100
2014-05-01
2
2
2014-06-01
2014-05-31
100
2014-06-01
3
3
2014-07-01
2014-07-01
200
2014-04-01
4
1
2014-05-01
2014-04-30
200
2014-05-01
5
2
2014-06-01
2014-05-31
200
2014-06-01
6
3
2014-07-01
2014-07-01
Gavin Clayton Claytabase Ltd www.claytabase.co.uk

Similar Messages

  • Substract one day to a date in a XSLT mapping

    Hi,
    I need to map a ENDDA field to a new field but subtracting a day, and reformatting the date, e.g
    from:
    <ENDDA>20090213</ENDDA>
    I need:
    <ENDDA>12.02.2009</ENDDA>
    For reformatting, sub string and concat with . is working, but how can I substract a day, including the handling where the date is the first day of the month?
    thx for your inputs

    I think this will help you.
    Create one UDF and insert 2 parameters.
    First parameter as your date
    second is number of hours 24:00:00
    Code:
       //write your code here
    SimpleDateFormat sd = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    SimpleDateFormat sdTarget = new SimpleDateFormat("MM/dd/yyyy");
    try {
    Date currentDate = sd.parse(a);
    Date compareDate = sd.parse(a.substring(0,10) + " " + b);
    if (currentDate.after(compareDate)  )
        Calendar c = Calendar.getInstance();
    c.setTime(sdTarget.parse(a));
    c.add(Calendar.DATE,1);
    a = sdTarget.format(c.getTime());     
    else
    //a = "before"
    a =  a.substring(0,10) ;
    catch(Exception e)
    return a;
    This might need little modification as your need.
    After that use Date Transformation function which will convert date in your required format.
    Thanks,
    Hetal
    Edited by: hetal shah on Feb 17, 2009 9:43 PM

  • How to add one day to a Date?

    Hi,
    What is the simplest way to daa one day to a Date object?
    Thanks.
    Pengyou

    You use a database query just to add one day
    to a Date object? I wouldn't call that the "simplest"
    way at all.I was going to suggest firing off an email to the naval observatory and having them carry out the calculation, emailing the result back (maybe using JMS, not sure - web service possibly?) but the OP's way is simpler.

  • How i will increment  one day with Selected date

    hi,
    I have written a code, in this code i have selected a date from calendar. Now i want to know how will increment one day with this selected date. IS there anyone who can help me? please help me.
    With regards
    Bina

    Hi Bina,
    Lets break the task into smaller steps.
    You can research on Google and search on how to Implement the select date or enter date with Javascript.
    Lets assume that the date has been entered into a text box inside a form inside the HTML code like this:
    <form action ="target.jsp">
       <input type="text" value="" name="dateField"/>
       <input type="submit" name="submit" value="submit" />
    </form>When the user clicks the submit button, the form gets submitted, then the form redirects the user to target.jsp
    Inside target.jsp you can add a date like this
    <%
        String dateField = request.getParameter("dateField");
        // Here use substring function to extract the day , month and year from
    // date field.
        Calendar cal = Calendar.getInstance();
        cal.set(year,month,day);
    // assuming you are storing days in an integer field
    int days;
    cal.add(days);
    %>Note that I have used JSP scriptlets, and the above code is somewhat pseudo-code. Since the task is big I suggest that you break it into smaller more specific tasks and then try to implement them by searching on Google for help first.
    If you get stuck with something specific you can ask here.

  • Date data from Crystal Report one day less than Date from Source

    Hi,
    I am creating a Crystal Report 4.0 for Enterprise Users (Client Version only) based on a BEx query. I have a field called 'Date' that I am bringing over from the BEx query. It has a format of MM/DD/YYYY. When I view the 'Date' in Crystal Reports, it is displaying as 1 day less from the actual data. For example, in the BEx Query, 'Date' = 11/01/2011.  In Crystal Reports, it is showing up as 10/31/2011, which is one day behind.  I'm not sure what's going on.
    Please help!

    Well, this is as weird as it gets!
    Here are the observations on SP02:
    1) The Date Characteristic is a day off in the report while the 'Key' shows correct date
    2) It displays in the '30.01.2011' (DD.MM.YYYY)format
    3) When you right-click this field to change the date format, the date option is missing
    4) You create a formula to see if this is a date and it returns True. The formula you can try is :
    isdate(). It returns False for any other function like isnumeric() etc
    5) So, this is a date filed, or CR4E thinks this is a date field, however doesn't let us change the format
    6) You cannot use this field with date functions like cdate(); weird!
    7) The only way you can show it the way you want is to write this formula:
    *mid(,4,2)&"/"&left(,2)&"/"&Right(,4)*
    This is still a string field, however displays as MM/DD/YYYY.
    In Crystal Reports 2011, however, the following formula on the same date field returns a date value, meaning you can further right-click this formula field to change the date format like a normal date field:
    cdate(tonumber(Right(,4)),tonumber(mid(,4,2)),tonumber(left(,2)))*
    The same formula, however, throws a 'String is non-numeric error in CR4E and I found that it doesn't like this:
    *tonumber(Right(,4)) ; same error
    I've tried a lot of combinations to make this a date field in CR4E, however none works!
    So, the only option is to use the formula that I mentioned above.
    P.S: If you install Patch 2.10, you'll notice that the Characteristic value shows the correct date. For some reason though, it is in the same format as the 'Key' and if we wish to change the date format, the only possible workaround is to extract the numbers and join like I suggested above.
    Hope this helps!
    -Abhilash

  • Add one day to current date

    Hi ,
    can anyone tell me the logic for add one day to sysdate()?
    Thanks and Regards,
    Ranjith.

    Hi Ranjith K,
    you can use sysdate()+1 to increment the system date.
    I hope this will help you out.
    Regards,
    Akhileshkiran.

  • Substract 1 day to a date, user-defined function.

    I want to sybstract 1 day from the given DATE.
    My userdefined function has only 1 argument which receives the given Date.
    argument name is " a "
    Imports :
    java.util.Date;java.text.*;java.util.Calendar;java.util.GregorianCalendar;
    public String dateTrans(String a,Container container){
    String DATE_FORMAT = "yyyyMMdd";
    java.text.SimpleDateFormat sdf =  new java.text.SimpleDateFormat(DATE_FORMAT);
    Calendar c1 = Calendar.getInstance();
    c1.set(Calendar.YEAR,2006);
    c1.set(Calendar.MONTH,6);
    c1.set(Calendar.DATE,7);
    c1.add(Calendar.DATE, -1);
    String s = sdf.format(c1.getTime());
    return s;
    My target field receiving the value " 20060706 "
    which was my defaut date - 1 day.
    Its not taking the value which I'm passing as an argument.
    can someone please look into it, as I'm not that good at "Java Programming "
    Thanks
    sagar

    Try this:
    import java.util.Date;
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    import java.text.DateFormat;
    DateFormat inputFormat = new SimpleDateFormat("yyyyMMdd");
              DateFormat outputFormat = new SimpleDateFormat("yyyyMMdd");
              //String da = myformat.format(new Date());
              Date ed = new Date();
              try {
                   Date sd = inputFormat.parse(a);
                   a = inputFormat.format(new Date());
                   Calendar cal = Calendar.getInstance();
                   cal.setTime(sd);
                   int num = -1;
                   cal.add(Calendar.DATE, num);
                   ed = cal.getTime();
                   return outputFormat.format(ed).toString();
              } catch (Exception e){
                   return "";
    Regards,
    Sandro

  • Need to subtract one day from the date appearing

    Hi, I am modifying a script in such a way that for an output type there is an invoice date appearing.  This invoice date should be back dated to 1 date less to the actually date that is appearing currently.
    For ex the date is 2007.08.30 it should appear as 2007.08.29.
    While debugging I found that it is picking up the data for the date from the structure vbdkr and the field is fkdat. From the following code in the script.
    /:   DEFINE &SALES_ORDER& := &VBDKR-VBELN_VAUF&
    /:   INVOICE DATE,, : &VBDKR-FKDAT&
    What changes do I need to make the changes in the script or in the driver program?
    Can you please suggest?
    Thanks.

    Hi..
    You can get this Functionality by Calling a FORM (subroutine) from the Script Layout itself  There is no need to change the Print program.
    Eg:
    In the layout set -> window -> text elements. Write this before displaying invoice date
    /: PERFORM F_DATE_SUB IN PROGRAM ZPRG01
    /: CHANGING &VBDKR-FKDAT&
    /: ENDPERFORM
    INVOICE DATE,, : &VBDKR-FKDAT&
    Create the report program ZPRG01: In the program ZPRG01
    FORM F_DATE_SUB TABLES INTAB STRUCTURE ITCSY
                                                  OUTTAB STRUCTURE ITCSY.
    DATA: L_DATE TYPE D.
    READ TABLE OUTTAB INDEX 1.
    L_DATE = OUTTAB-VALUE. "you need to convert here
    SUBTRACT  1 FROM L_DATE.
    OUTTAB-VALUE = L_DATE.
    MODIFY OUTTAB INDEX 1.
    ENDFORM.
    reward if Helpful.

  • I backed up my data 3 times in one day does all data from those backups get restored?

    Does everything backup on a certain date get restored?

    I'm using iCloud to backup my iOS device (an iPad). I believe it's using the latest backup. SO ... the latest backup and what data is in it, will be what I get back again, if I do a restore.

  • Substract days to a date

    Does somebody know a method to substract to a given date (in String format dd/mm/yyyy) a given number of days??
    public String restarDias(String fecha, int dias) throws Exception {
    DateFormat df = DateFormat.getDateInstance();
    Date fechaD = df.parse(fecha);
    return dateResult;
    }.. thanks!

    Hi,
    you can do this easily when you define a Calendar and then apply the method:
    //somewhere in code
    //Calendar Cal = Calendar.getInstance();
    Cal.add(Calendar.DATE,3); //to add 3 days
    Cal.add(Calendar.DATE,-1);//to substract one day
    greetings,
    alina3

  • Add one day to java.sql.date

    I ask user to enter a date: yyyy-mm-dd, then I want to add one day to that date, I don't know how to do it?
    java.sql.Date time2 = java.sql.Date.valueOf( args[0] );
    Many Thanks.

    since a date object is really nothing more than a long you can always add one day's worth of milliseconds to it....
    long milliInADay = 1000 * 60 * 60 * 24;
    Date d = ......
    d.setTime(d.getTime() + milliInADay);although there may be some issues with this i'm unaware of, but seems pretty straightforward to me.

  • Strange one day adding to tasks due date (Outlook –SharePoint- MS Project integration)

    Hi,
    I have integrated MS Project 2010 with MS Outlook 2010 via SharePoint. Everything has gone more or less smoothly (as it could be for Microsoft
    J), but finally I got into a strange problem. Strangely enough,
    MS Outlook, while syncing tasks with SharePoint (these tasks are pre-created in MS Project and synced with SharePoint beforehand),
    adds one day to due dates of each of these tasks, e.g. for “Task 1” due date in MS Project and on SharePoint is 20.10.2010, but MS Outlook puts it as 21.10.2010, for “Task 2” due date in MS Project and on SharePoint is 22.10.2010, but MS Outlook
    puts it as 23.10.2010, etc.
    What is a strange trick? I have checked work time, holidays - everything is the same in MS Outlook, SharePoint and MS Project (8-17, Mon-Fri, 8 hours, 40 days a week). What could it be?
    Thanks a lot.

    Hi,
    It seems nobody can reply, so I will reply by myself because I found out the answer. The answer from Microsoft is pretty simple - ha-ha we know about,
    we will work on it, wait, ha-ha. Firstly let me quote Microsoft senior tech answer on the same request 3 month ago:
    "Hello Artur,
    The senior tech got back to me with the following conclusion:
    Outlook is rounding everything set to 12:00 PM or after to the next date because the columns mapped to start date and due dates are only Date columns,
    not date/time columns.
    It is rounding at noon because of the .5 decimal. This can be seen in Excel - 41115.5 = 7/25/2012 12:00 in date/time format whereas 41115.49 = 7/25/2012
    11:45.
    The issue seems to be an expected behavior, the next level of support is working on the same but currently I have not been provided with any ETA, so
    it looks like the issue will take a good amount of time to get fixed.
    I would appreciate your patience while we are working on the issue. I will keep you posted as and when I get an update on the issue."
    Sounds good, does not it? Ok. After asking this question recently I got run-around and finally I found the same question unsolved (see the quotation
    before) more than 3 month ago. I have sent it to Microsoft Tech Support. Look at reply I got from them on 22.10.2012:
    "This issue has been noticed and will be handled by the product team. Once we have any updates, it will be responded to you timely". 
    So the basic function does not work properly, there month passed - no way! Wait until what? I could hardly
    even imagine how Microsoft launched the product with such a bug and is not resolving the problem for months.

  • Adding one day to a month

    Hi,
    I would like to add one day to a date. It seems like such a simple thing yet I have only found how to add a day to sysdate (or to add months to a given date).
    Any help?
    Thanks.
    Leah

    Hi,
    If you want a specific date then it should be in date type,
    so you cannot do: '01-MAR-2011'+1
    but you can do: to_date('01-MAR-2011')+1
    it seems to me that you just tried to add 1 to a string looking like a date
    Tamir

  • Add one day to a binding

    Hi all,
    I assume this is a very dumb question... How can I add one day to a date that is like this :
    #{bindings.ChooseDateCrah.inputValue}
    It's a binding in a the header text property of a column.
    That : #{bindings.ChooseDateCrah.inputValue + 1} or that : #{bindings.ChooseDateCrah.inputValue} +1 does not work.
    Thanks.
    Frédéric.

    Hi Frédéric,
    Yeah, you cannot apply a converter on the headerText attribute of a column. However, you can instead place an outputText in the header facet of the column, then the converter option still works.
    As for the code, no, in the RowImpl object in the getter method of your calculated attribute. (That method is quite complicated for a read only value though).
    You can check my reply at Re: How to access menbers of the current row of an af:table component? as for how to create a custom converter. In your specific case, however, you would have to extends DateTimeConverter and decorate the getAsObject and getAsString methods in order to add one day before calling super.getAsString and remove one day from the returned value of super.getAsObject before returning to ensure reversibility.
    A managed bean solution would imply the use of the implements Map EL hack which is both easy and hard, depending on your experience level with JSF and EL.
    Regards,
    ~ Simon

  • SAP Work Manager 6.0 - Creating notification on mobile device (customizing the default Start and End date)

    Dear all,
    When I create a notification in SAP Work Manager 6.0, the notification gets created in the back-end with Start Date equal to Current Date + 1 day and End Date equal to Current Date + 3 days.
    Can you advise where I can customize this behaviour, so that the Start Date is for example equal to the Current Date and End Date being 14 days in the future? Is this something that would need to be configured in Agentry Editor or within the ABAP stack?
    Many thanks for your support.
    Pavel
    Tags edited by: Michael Appleby

    Hi Jason,
    We went this route and identified the initial values, and tried to modify those.
    No impact though, as if those values are being overwritten elsewhere (whatever value we set, no change on the backend).
    Please see the attachments with screenshots of Agentry Editor.
    Many thanks.
    Pavel

Maybe you are looking for