Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PL/SQL How to get X day ago from a Date as Date?

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

How to do it with pl/sql?

like image 861
yetAnotherSE Avatar asked Nov 22 '12 09:11

yetAnotherSE


People also ask

How do I subtract days from a date in SQL Developer?

Use sysdate-1 to subtract one day from system date.

How do I subtract dates in Oracle?

Use the @DATEDIFF function to calculate the difference between two dates or datetimes, in days or seconds. The difference between the specified dates. Valid values can be: DD , which computes the difference in days.

How do you subtract working days in SQL?

If you only care about weekends and ignore holidays, you can use the following formula: DATEADD(DAY, -7*(@bdays / 5) + @bdays % 5, @start_date) . Then if the result falls on Saturday subtract 1 day, if it falls on Sunday subtract 2 days.

How do I get days between two dates in SQL?

The DATEDIFF() function returns the difference between two dates.


1 Answers

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

    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
    

OR

     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
like image 114
Nick Krasnov Avatar answered Sep 29 '22 23:09

Nick Krasnov