Thursday, 15 August 2013

To delete a row when sysdate is a certain value in oracle sql -


After having existed for a certain time, there is a problem deleting a line in my table to be more specific For, I have a column called 'hiredate' and I want to remove that line when the boat on that line is older than 5 years.

In relation to your comment in your comment:

  Emp, where p_id = (select p_id from emp where sysdate = hiredate + 1825);  

To use a subcube here, it is absolutely unnecessary that you can do this:

Remove from AMP WHERE sysdate = hiredate + 1825;

Of course, this will only work where the time part of hiredate is exactly the same as the time portion of SYSDATE ! In addition, 1825 is not five years; This is five times 365. There will be a better way to do this in Oracle:

  remove emp WHERE from hiredate & lt; TRUNC (ADD_MONTHS (SYSDATE, -60)); Or, if you are certain that you want to delete only one day's results:  
  AMP has been rented from where; = TRUNC (ADD_MONTHS (SYSDATE), -60)) - 1 more job lieutenant; TRUNC (ADD_MONTHS (SYSDATE, -60));  

However, if you are running it as a one-time query or even as a scheduled job, you want to delete everything at the age of five; A scheduled job, for example, can always fail in one night and can be successful next, leaving you with a weird day record in EMP .

By the way, ADD_MONTHS () is the safest way to add or subtract years in Oracle; Use of intervals from year to month during the leap years can fail. For example, the following will fail on February 29, 2016:

  From where AMP has been hired and removed; TRUNC (SYSDATE) - Interval '5' year;  

The right side of the equation will calculate February 29, 2011, which is not in existence.


No comments:

Post a Comment