このクエリはエラーで機能します
select add_months(date '2011-01-31', 1) from dual;
、これは:
select date '2011-01-31' + interval '1' month from dual;
戻り値
ORA-01839: date not valid for month specified
INTERVAL
では、リテラルを使用して間隔を追加する安全な方法はありますか?
このクエリはエラーで機能します
select add_months(date '2011-01-31', 1) from dual;
、これは:
select date '2011-01-31' + interval '1' month from dual;
戻り値
ORA-01839: date not valid for month specified
INTERVAL
では、リテラルを使用して間隔を追加する安全な方法はありますか?
これは、日付に s を追加するというANSI 仕様の動作1INTERVAL
に従います。これはここにも文書化されています:
間隔の計算で日時値が返される場合、結果は実際の日時値である必要があります。そうでない場合、データベースはエラーを返します。たとえば、次の 2 つのステートメントはエラーを返します。
SELECT TO_DATE('31-AUG-2004','DD-MON-YYYY') + TO_YMINTERVAL('0-1') FROM DUAL;
SELECT TO_DATE('29-FEB-2004','DD-MON-YYYY') + TO_YMINTERVAL('1-0') FROM DUAL;
一方、この関数ADD_MONTHS
は、結果の月の日数が少ない場合、月の最終日を提供するだけです。この関数は、この問題に対処するために作成されたと思います。
1 http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
b) Arithmetic is performed so as to maintain the integrity of
the datetime data type that is the result of the <datetime
value expression>. This may involve carry from or to the
immediately next more significant <datetime field>. If the
data type of the <datetime value expression> is TIME, then
arithmetic on the HOUR <datetime field> is undertaken modulo
24. If the <interval value expression> or <interval term> is
a year-month interval, then the DAY field of the result is
the same as the DAY field of the <datetime term> or <datetime
value expression>.
c) If, after the preceding step, any <datetime field> of the
result is outside the permissible range of values for the
field or the result is invalid based on the natural rules for
dates and times, then an exception condition is raised: data
exception-datetime field overflow.