title: Interval arithmetic to correctly deal with end of month - Oracle SQL
body: <p>I need a function which will do interval arithmetic, dealing "correctly" with the different number of days in a month.  For my version of "correctly" - see below!</p>
  
  <p><strong>First try</strong></p>
  
  <pre><code>select to_date('31-May-2014') + interval '1' months from dual
  </code></pre>
  
  <p>This returns an error, because there is no 31st June.  I understand that this behaviour is expected due to the ANSI standard.</p>
  
  <p><strong>Second try</strong></p>
  
  <pre><code>select add_months(to_date('31-May-2014'),1)  from dual
  </code></pre>
  
  <p>This correctly (in my use case) returns 30th June 2014, which is great.  BUT</p>
  
  <pre><code>select add_months(to_date('28-Feb-2014'),1)  from dual
  </code></pre>
  
  <p>returns 31st March 2014, when I want 28th March 2014.</p>
  
  <p><strong>Background</strong></p>
  
  <p>This has to do with legal deadlines.  The deadlines are expressed in law as a number of months (say, 3) from a base date.  If the base date is last day of the month and three months later the month is longer, then the deadline does NOT extend to the end of the longer month (as per the add_months function).  However, if the base date is last day of the month and three months later the month is shorter, then the deadline expires on the last day of the shorter month.</p>
  
  <p><strong>Question</strong></p>
  
  <p>Is there a function that does what I need?</p>
  
  <p>I have intervals (year to month) stored in a table, so preferably the function would look like:</p>
  
  <p>add_interval_correctly(basedate DATE, intervaltoadd INTERVAL YEAR TO MONTH)</p>