Home » SQL & PL/SQL » SQL & PL/SQL » Add # of days and then pass it as a variable (Oracle 11g release 2)
Add # of days and then pass it as a variable [message #624793] Thu, 25 September 2014 13:48 Go to next message
azeem87
Messages: 116
Registered: September 2005
Location: dallas
Senior Member
Hi,

try to get max of a date column and then add 15 days and pass this calculated date as a where clause, tried with to_date, and to _char
getting invalid month error.


CREATE OR REPLACE PROCEDURE move_hist
IS
      move_Dates  varchar2(15);
BEGIN
 select to_char(max(effectivedate),'DD-MON-YYYY' )+15  into move_Dates  from  txn_hist; 

     INSERT      /*+ APPEND NOLOGGING*/
         INTO    txn_temp(              txn_temp,
                                         tnum_date,
                                         TYPE)
      SELECT tnum, effectiveDate, 'P'
        FROM PRNT_TXN       WHERE     STATUS IN ('EX', 'DL')
              AND  (effectivedate) < move_Dates  AND product = 'INTL';

   COMMIT;
end;



Please suggest the option i should be using to get max then 15 days and pass it on where clause,
effectivedate in source table is of DATE datatype.

Thanks
Re: Add # of days and then pass it as a variable [message #624794 is a reply to message #624793] Thu, 25 September 2014 14:00 Go to previous messageGo to next message
joy_division
Messages: 4963
Registered: February 2005
Location: East Coast USA
Senior Member
You are adding the number 15 to a string.

Why would you use a TO_DATE on a DATE column?
Why would you try to add a number to a string?

That is like saying what is peanut butter + 15.

[edit] fixed kooky typo.

[Updated on: Thu, 25 September 2014 14:02]

Report message to a moderator

Re: Add # of days and then pass it as a variable [message #624795 is a reply to message #624793] Thu, 25 September 2014 14:01 Go to previous messageGo to next message
BlackSwan
Messages: 26766
Registered: January 2009
Location: SoCal
Senior Member
SQL> select empno, hiredate from emp where hiredate < (select max(hiredate)+15 from emp);

     EMPNO HIREDATE
---------- ---------
      7369 17-DEC-80
      7499 20-FEB-81
      7521 22-FEB-81
      7566 02-APR-81
      7654 28-SEP-81
      7698 01-MAY-81
      7782 09-JUN-81
      7788 19-APR-87
      7839 17-NOV-81
      7844 08-SEP-81
      7876 23-MAY-87

     EMPNO HIREDATE
---------- ---------
      7900 03-DEC-81
      7902 03-DEC-81
      7934 23-JAN-82

14 rows selected.

SQL> 




Are you sure this is what you really want?
Re: Add # of days and then pass it as a variable [message #624797 is a reply to message #624795] Thu, 25 September 2014 14:48 Go to previous messageGo to next message
azeem87
Messages: 116
Registered: September 2005
Location: dallas
Senior Member
thanks all for your time,
what i am trying to get is get the max(date)+15 and get this date in where clause.
based on that inner select you suggested i have this, please advice any suggestions on this.

CREATE OR REPLACE PROCEDURE move_hist
IS
      move_Dates  varchar2(15);
BEGIN

     INSERT      /*+ APPEND NOLOGGING*/
         INTO    txn_temp(              txn_temp,
                                         tnum_date,
                                         TYPE)
      SELECT tnum, effectiveDate, 'P'
        FROM PRNT_TXN       WHERE     STATUS IN ('EX', 'DL')
              AND  (effectivedate) < (select max(effectivedate)+16 from txn_hist)  AND product = 'INTL';

   COMMIT;
   
      INSERT    /*+ APPEND NOLOGGING*/
         INTO   Qevents_hist
      SELECT *    FROM Qevents
       WHERE   (ENTEREDTIME) <  (select max(ENTEREDTIME)+15 from Qevents_hist) ;
    	   
      DELETE FROM QEVENTS
      WHERE    (ENTEREDTIME) <  (select max(ENTEREDTIME)- 15 from Qevents_hist);  
	  **** Here i have to use - ( minus ) because above insert will already insert the rows into Qevents_hist so ENTEREDTIME will be changed         	 
commit;
	  
end;
/




Re: Add # of days and then pass it as a variable [message #624798 is a reply to message #624795] Thu, 25 September 2014 14:50 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
 INSERT      /*+ APPEND NOLOGGING*/
         INTO    txn_temp(              txn_temp,
                                         tnum_date,
                                         TYPE)
      SELECT tnum, effectiveDate, 'P'
        FROM PRNT_TXN       WHERE     STATUS IN ('EX', 'DL')
              AND  (effectivedate) < select max(effectivedate)+15 from  txn_hist)
 AND product = 'INTL';
Re: Add # of days and then pass it as a variable [message #624801 is a reply to message #624798] Thu, 25 September 2014 14:56 Go to previous message
azeem87
Messages: 116
Registered: September 2005
Location: dallas
Senior Member
thanks Bill B and All..

[Updated on: Thu, 25 September 2014 15:45]

Report message to a moderator

Previous Topic: order by query in sql
Next Topic: stored procedure
Goto Forum:
  


Current Time: Fri May 10 09:24:00 CDT 2024