我有一个日期,我想打印该日期的偏移量。我可以这样做:
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
还有其他方法吗?
最佳答案
您可能想使用 NumToYMInterval 函数
declare
v_interval pls_integer := 4;
begin
dbms_output.put_line( sysdate - NumToYMInterval( v_interval, 'month' ) );
end;
/
关于oracle - PL/SQL 和日期间隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4106619/