PL/SQL 如何从日期中获取 X 天前的日期?

我想从 08-APR-13 获得 100 天前的日期.

I want to get 100 days ago from 08-APR-13, as date.

pl/sql 怎么做?

How to do it with pl/sql?

推荐答案

假设 08-APR-13 在您的情况下是一个字符串.因此,您需要使用 to_date 函数将其转换为 date,然后简单地减去 100 文字.

Assumption was made that the 08-APR-13 is a string in your situation. So you need convert it to date using to_date function, and then simply subtract 100 literal.

  • SQL

SQL> select (to_date('08-APR-13', 'DD-MON-RR') - 100) res
  2    from dual
  3  /

RES
-----------
29-12-2012

  • PL/SQL

  • PL/SQL

    SQL> declare
      2    l_res_date date;
      3    l_in_date  varchar2(11) := '08-APR-13';
      4  begin
      5    select (to_date(l_in_date, 'DD-MON-RR') - 100)
      6      into l_res_date
      7    from dual;
      8  
      9    dbms_output.put_line(to_char(l_res_date, 'dd-mon-yy'));
      10  end;
      11  /
    
      29-dec-12
    
      PL/SQL procedure successfully completed
    

  •      SQL> declare
           2    l_res_date date;
           3    l_in_date  varchar2(11) := '08-APR-13';
           4  begin
           5  
           6    l_res_date := to_date(l_in_date, 'DD-MON-RR') - 100;
           7  
           8    dbms_output.put_line(to_char(l_res_date, 'dd-mon-yy'));
           9  end;
           10  /
    
           29-dec-12
    
           PL/SQL procedure successfully completed
    

    相关文章