Oracle Date - How to add years to date

后端 未结 7 2033
温柔的废话
温柔的废话 2020-12-05 17:38

I have a date field

DATE = 10/10/2010

sum = 4 (this are number of years by calculation)

is there a way to add four years to 10/10

相关标签:
7条回答
  • 2020-12-05 17:38

    I am not sure, if I understood Your question correctly, but

    select add_months(someDate, numberOfYears * 12) from dual
    

    might do the trick

    0 讨论(0)
  • 2020-12-05 17:38

    One more option apart from ADD_MONTHS

    SELECT
          SYSDATE,
          SYSDATE
          + TO_YMINTERVAL ( '1-0' )
    FROM
          DUAL;
    
    
    SYSDATE   SYSDATE+TO_YMINTERVAL('1-0')
    --------- ----------------------------
    29-OCT-13 29-OCT-14                   
    1 row selected.
    
    
    SELECT
          SYSDATE,
          SYSDATE
          + TO_YMINTERVAL ( '2-0' )
    FROM
          DUAL;
    
    
    SYSDATE   SYSDATE+TO_YMINTERVAL('2-0')
    --------- ----------------------------
    29-OCT-13 29-OCT-15                   
    1 row selected.
    
    SELECT
          TO_DATE ( '29-FEB-2004',
                  'DD-MON-YYYY' )
          + TO_YMINTERVAL ( '1-0' )
    FROM
          DUAL
    
       *
    Error at line 4
    
    ORA-01839: date not valid for month specified
    

    But the last one is illegal since there is no 29th day of February in 2005, hence it fails on leap year cases (Feb 29)

    Read the documentation for the same

    0 讨论(0)
  • 2020-12-05 17:41

    I believe you could use the ADD_MONTHS() function. 4 years is 48 months, so:

    add_months(DATE,48)
    

    Here is some information on using the function:

    http://www.techonthenet.com/oracle/functions/add_months.php

    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:1157035034361

    0 讨论(0)
  • 2020-12-05 17:47

    Try adding months (12 * number of years) instead. Like this-

    add_months(date'2010-10-10', 48)
    
    0 讨论(0)
  • 2020-12-05 17:50
            SELECT TO_CHAR(SYSDATE,'YYYY')-2 ANO FROM DUAL
    
    0 讨论(0)
  • 2020-12-05 17:51

    Use add_months

    Example:

    SELECT add_months( to_date('10-OCT-2010'), 48 ) FROM DUAL;
    

    Warning
    add_months, returns the last day of the resulting month if you input the last day of a month to begin with.

    So add_months(to_date('28-feb-2011'),12) will return 29-feb-2012 as a result.

    0 讨论(0)
提交回复
热议问题