IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

delete records from table if record is > (1year + 1month)


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
alluri12

New User


Joined: 15 Aug 2006
Posts: 44
Location: Virginia, USA

PostPosted: Wed Dec 02, 2009 7:54 am
Reply with quote

Hi,
I have a table

Code:
Table1
 col1         char(2)
 col2         decima(2)
 col3         char(2)
 Active_dt    date
 col5         char(2)
 col6         char(2)


The active_dt is the date when we insert the record.
I want to delete the records from this table if the Active_dt older than (1year + 1month).

I request if somebody can help me in writing the Sql for this.
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Wed Dec 02, 2009 8:06 am
Reply with quote

What have you tried so far and what results have you gotten
Back to top
View user's profile Send private message
alluri12

New User


Joined: 15 Aug 2006
Posts: 44
Location: Virginia, USA

PostPosted: Wed Dec 02, 2009 8:15 am
Reply with quote

I tried
delete from table1
where days(current date) - days(active_dt) > 395

I thought 1 year = 365 days
1 month = 30 days
I know this is not correct b.c days for the month changes to 30 or 31 and for the year it will 365 or 366
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Dec 02, 2009 8:46 am
Reply with quote

why don't you try something along the line of current-date - 13 months > active_dt
Back to top
View user's profile Send private message
dick scherrer

Moderator Emeritus


Joined: 23 Nov 2006
Posts: 19244
Location: Inside the Matrix

PostPosted: Wed Dec 02, 2009 8:47 am
Reply with quote

Hello,

You need to define exactly what 1 year is to your organization. Same with 1 month. Diferent organizations have different date rules.
Back to top
View user's profile Send private message
alluri12

New User


Joined: 15 Aug 2006
Posts: 44
Location: Virginia, USA

PostPosted: Wed Dec 02, 2009 9:00 am
Reply with quote

Thank you Dick Brenholtz for a fast respnose. It works....
Back to top
View user's profile Send private message
dbzTHEdinosauer

Global Moderator


Joined: 20 Oct 2006
Posts: 6966
Location: porcelain throne

PostPosted: Wed Dec 02, 2009 9:02 am
Reply with quote

Quote:
You need to define exactly what 1 year is to your organization. Same with 1 month. Diferent organizations have different date rules.


when we are talking about financial calculations, I agree.

but this is an archival/history thing and 13 months from the current date, is always going to be 13 months from the current date. and because it is the current date, db2 will always calculate the 'correct date' based on today's date.

but, I agree, also, that the TS should be aware of the organization's date definitions.

and I wanted to be the last poster of this thread
Back to top
View user's profile Send private message
alluri12

New User


Joined: 15 Aug 2006
Posts: 44
Location: Virginia, USA

PostPosted: Wed Dec 02, 2009 10:07 am
Reply with quote

The requirements that I got for this code is from the current date. So this code works.... I too agree differnt org has different date rules
Back to top
View user's profile Send private message
alluri12

New User


Joined: 15 Aug 2006
Posts: 44
Location: Virginia, USA

PostPosted: Wed Dec 02, 2009 7:45 pm
Reply with quote

I found another solution also for this requriement. It is almost same as what dick gave... but just want to share...

Code:
Delete  From table1
WHERE DAYS(Active_dt) < DAYS((CURRENT DATE - 1 YEAR) - 1 MONTH)
Back to top
View user's profile Send private message
GuyC

Senior Member


Joined: 11 Aug 2009
Posts: 1281
Location: Belgium

PostPosted: Wed Dec 02, 2009 8:03 pm
Reply with quote

alluri12 wrote:
I found another solution also for this requriement. It is almost same as what dick gave... but just want to share...

Code:
Delete  From table1
WHERE DAYS(Active_dt) < DAYS((CURRENT DATE - 1 YEAR) - 1 MONTH)

days(date1) < days(date2)
is like
Col1 + 1000 < Col2 +1000

it is correct but more sense and - far more important - much more performant is

date1 < date2
col1 < col2
Back to top
View user's profile Send private message
Craq Giegerich

Senior Member


Joined: 19 May 2007
Posts: 1512
Location: Virginia, USA

PostPosted: Wed Dec 02, 2009 9:09 pm
Reply with quote

Instead of
Code:
WHERE DAYS(Active_dt) < DAYS((CURRENT DATE - 1 YEAR) - 1 MONTH)


I think
Code:
WHERE Active_dt < (CURRENT DATE - 1 YEAR) - 1 MONTH


would be more efficient, evaluating (current date - 1 year) - 1 month would only have to be done once, but evaluating date(active_dt) would have to be done for every row before it could be tested.
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts Compare 2 files and retrive records f... DFSORT/ICETOOL 3
No new posts Compare 2 files(F1 & F2) and writ... JCL & VSAM 8
No new posts How to split large record length file... DFSORT/ICETOOL 10
No new posts DELETE SPUFI DB2 1
No new posts Load new table with Old unload - DB2 DB2 6
Search our Forums:

Back to Top