PL/SQL 和日期间隔

发布于 2024-09-30 11:13:44 字数 1315 浏览 7 评论 0原文

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

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

还有其他方法可以做到这一点吗?

I have a date and I'd like to print the offset from that date. I can do this:

dbms_output.put_line(to_char(g_startDate - interval '4' month ,'YYYY-MM-DD'));

and it works fine. The problem is that the interval is variable. When I try this:

dbms_output.put_line(to_char(g_startDate - interval g_dateOffsetAmt month ,'YYYY-MM-DD'));

I get a compiler error.

I thought it might be because g_dateOffsetAmt is an integer so I tried this:

dbms_output.put_line(to_char(g_startDate - interval to_char(g_dateOffsetAmt) month ,'YYYY-MM-DD'));

Though I still get compiler errors saying:


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

Is there some other way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅黛梨妆こ 2024-10-07 11:13:44

您可能想使用 NumToYMInterval 函数

declare
  v_interval pls_integer := 4;
begin
  dbms_output.put_line( sysdate - NumToYMInterval( v_interval, 'month' ) );
end;
/

You would probably want to use the NumToYMInterval function

declare
  v_interval pls_integer := 4;
begin
  dbms_output.put_line( sysdate - NumToYMInterval( v_interval, 'month' ) );
end;
/
夏末 2024-10-07 11:13:44

有几种方法可以做到这一点。要么输入作为间隔传入的变量,要么使用函数 add_months 代替:

declare
    v_interval INTERVAL YEAR TO MONTH := interval '4' month;
begin
    dbms_output.put_line(to_char((sysdate - v_interval), 'MM/DD/YYYY'));
end; 

declare
    v_interval PLS_INTEGER := 4;
begin
    dbms_output.put_line(to_char(add_months(sysdate, -v_interval), 'MM/DD/YYYY'));
end; 

There are a couple ways to do this.. Either type your variable being passed in as an interval, or use the function add_months instead:

declare
    v_interval INTERVAL YEAR TO MONTH := interval '4' month;
begin
    dbms_output.put_line(to_char((sysdate - v_interval), 'MM/DD/YYYY'));
end; 

declare
    v_interval PLS_INTEGER := 4;
begin
    dbms_output.put_line(to_char(add_months(sysdate, -v_interval), 'MM/DD/YYYY'));
end; 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文