Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL and date intervals

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?

like image 588
FrustratedWithFormsDesigner Avatar asked Apr 18 '26 02:04

FrustratedWithFormsDesigner


1 Answers

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;
/
like image 73
Justin Cave Avatar answered Apr 20 '26 00:04

Justin Cave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!