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
还有其他方法可以做到这一点吗?
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 技术交流群。

发布评论
评论(2)
夏末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;
~没有更多了~
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
您可能想使用 NumToYMInterval 函数
You would probably want to use the NumToYMInterval function