|
View previous topic :: View next topic
|
| Author |
Message |
zh_lad
Active User
Joined: 06 Jun 2009 Posts: 115 Location: UK
|
|
|
|
Dear all,
Can you please provide me a query where I can delete rows if value on End_Date column is older than 3 month.
Many thanks,
ZH Lad |
|
| Back to top |
|
 |
ashimer
Active Member

Joined: 13 Feb 2004 Posts: 551 Location: Bangalore
|
|
|
|
| What have you tried ? |
|
| Back to top |
|
 |
zh_lad
Active User
Joined: 06 Jun 2009 Posts: 115 Location: UK
|
|
|
|
I have tried following queries but all gave me some syntax error:
SELECT * FROM HIGHQAL.TAB
WHERE G4852_END_DT < DATE_SUB(NOW(), INTERVAL 3 MONTH)
SELECT * FROM HIGHQAL.TAB
WHERE G4852_END_DT < DATEADD(MONTH, -3, GETDATE())
SELECT * FROM HIGHQAL.TAB
WHERE DATEDIFF(day, G4852_END_DT, now()) > 90 |
|
| Back to top |
|
 |
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
| You might want to read a DB2 manual / chapter on date functions and special registers, instead of an Oracle one. |
|
| Back to top |
|
 |
zh_lad
Active User
Joined: 06 Jun 2009 Posts: 115 Location: UK
|
|
|
|
DELETE * FROM HIGHQAL.TAB
WHERE (DAYS(END_DT) - DAYS(CURRENT DATE)) > 90
Seems it is working.
Thanks. |
|
| Back to top |
|
 |
guptae
Moderator

Joined: 14 Oct 2005 Posts: 1209 Location: Bangalore,India
|
|
|
|
Hello ZH_lad,
Read the manuals .Below hint might help
| Code: |
SELECT *
FROM table_name
WHERE col_1 < CURRENT DATE- 3 MONTHS
WITH UR; |
|
|
| Back to top |
|
 |
zh_lad
Active User
Joined: 06 Jun 2009 Posts: 115 Location: UK
|
|
|
|
| I have got the previous answer from manual. |
|
| Back to top |
|
 |
GuyC
Senior Member
Joined: 11 Aug 2009 Posts: 1281 Location: Belgium
|
|
|
|
Well, now that you have learned something, some extra advice :
using functions on columns in the where clause is not good for performance !
so
WHERE end_dt < CURRENT DATE- 3 MONTHS
is faster than
WHERE (DAYS(END_DT) - DAYS(CURRENT DATE)) > 90 |
|
| Back to top |
|
 |
Kjeld
Active User

Joined: 15 Dec 2009 Posts: 365 Location: Denmark
|
|
|
|
| GuyC wrote: |
Well, now that you have learned something, some extra advice :
using functions on columns in the where clause is not good for performance !
so
WHERE end_dt < CURRENT DATE- 3 MONTHS
is faster than
WHERE (DAYS(END_DT) - DAYS(CURRENT DATE)) > 90 |
...because you force DB2 to retrieve every row from data pages to do the function calculation before comparing to the literal value 90. The first construct can be calculated to a fixed value before data query starts, so DB2 can use an index value in the comparaison, if END_DT is part of an index, which it should be.
Before doing this kind of bulk deletes, you should check with your DBAs if the row counts involved should warrant a unload with a subsequent load of only the rows to be retained. |
|
| Back to top |
|
 |
|
|