我有一個日期,我想打印該日期的偏移量。我可以這樣做:PL/SQL和日期間隔
dbms_output.put_line(to_char(g_startDate - interval '4' month ,'YYYY-MM-DD'));
它工作正常。問題是間隔是可變的。當我嘗試這個:
dbms_output.put_line(to_char(g_startDate - interval g_dateOffsetAmt month ,'YYYY-MM-DD'));
我得到一個編譯器錯誤。
我想這可能是因爲g_dateOffsetAmt
是integer
所以我嘗試這樣的:
dbms_output.put_line(to_char(g_startDate - interval to_char(g_dateOffsetAmt) month ,'YYYY-MM-DD'));
雖然我仍然得到編譯器錯誤說:
Error: PLS-00103: Encountered the symbol "TO_CHAR" when expecting one of the following: .) , * @ & | = - + at in is mod remainder not rem => .. or != or ~= >= and or like LIKE2_ LIKE4_ LIKEC_ as between from using || member SUBMULTISET_ The symbol "," was substituted for "TO_CHAR" to continue. Line: 704 Error: PLS-00103: Encountered the symbol "MONTH" when expecting one of the following: . () , * % & | = - + at in is mod remainder not range rem => .. or != or ~= >= and or like LIKE2_ LIKE4_ LIKEC_ between || multiset member SUBMULTISET_ The symbol "." was substituted for "MONTH" to continue. Line: 704
有一些其他的方式來做到這一點?
是的,'add_months'完全正是我想要的。並認爲我以前使用它,只是忘了它:(男人我真的需要週末! – FrustratedWithFormsDesigner 2010-11-05 14:21:42
並使用添加月份繞過ORA-01839惱人的問題時使用區間算術在月底,例如' date'2010-03-30' - interval'1'month'。但add_months在月末也有自己的問題,如果你比較'add_months(date'2010-02-27',1)'和'add_months(date'2010-02-28',1)' – 2010-11-05 16:15:38