oracle - PL/SQL 和日期间隔

标签 oracle plsql intervals

我有一个日期,我想打印该日期的偏移量。我可以这样做:

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/

相关文章:

java - DriverManager.getConnection() 返回 null,而不是连接对象

java - JDBC + PL/SQL = 这么简单,还是有陷阱?

database - 如何将打包类型用作表中的列类型?

oracle - 如何在 Oracle 10g 中获取无效对象的错误列表

MySQL。在月末开始新行

sql - 在 Oracle SQL 中删除 HTML 标签的更好方法

sql - 在SQL中减去前一行减去当前行

oracle - 如何为 PL/SQL block 的表达式中的变量赋值?

python时间间隔算法求和

algorithm - 在区间 : does this approach work? 中寻找点的替代贪婪策略