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

Difference between two db2 dates in years


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
Debasis Misra
Warnings : 1

New User


Joined: 16 Sep 2008
Posts: 72
Location: Bangalore

PostPosted: Tue Apr 07, 2009 10:02 am
Reply with quote

I wanted to find the difference between two dates such that the result is in years

For Eg: '2001-12-31' - '2000-01-01' ~ 2yrs

When i use the query

SELECT YEAR('2001-12-31') - YEAR('2000-01-01')
FROM SYSIBM.SYSDUMMY1

I get the difference as 1 yr as this subtracts 2001-2000 = 1 yr

Is there any way that i can get the approximate difference in years between two dates?
Back to top
View user's profile Send private message
kregen

New User


Joined: 16 Mar 2006
Posts: 21

PostPosted: Tue Apr 07, 2009 6:15 pm
Reply with quote

i don't understand what you exactly mean!

when you subtract two date you get a result like this



Code:
SELECT DATE('2010-05-31') - DATE('2000-07-01')
FROM SYSIBM.SYSDUMMY1                         
;         
                                     
091030,



yymmdd =

first two digits = the difference of year's
second tow digits = the difference of month's
last tow digits = the difference of day's

i hope it's help!

cu
kregen
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: Tue Apr 07, 2009 9:43 pm
Reply with quote

Hello,

Notice that the original sql used "year" and the suggestion uses "date".

Quite a difference. . . .
Back to top
View user's profile Send private message
enrico-sorichetti

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Tue Apr 07, 2009 10:33 pm
Reply with quote

first define the requirement, and then let' s look at the algorithm

whatever the query, You will have to carry on additional procesing for the rounding

( that' s what looks like You are asking for )

given the result as yyyy/mm/dd You will have to compute

years = yyyy ( if mm <= 6 )
years = yyyy + 1 if ( mm > 6 )
Back to top
View user's profile Send private message
Debasis Misra
Warnings : 1

New User


Joined: 16 Sep 2008
Posts: 72
Location: Bangalore

PostPosted: Wed Apr 08, 2009 11:01 am
Reply with quote

The requirement was to check whether a given date yyyy-mm-dd was more than 2yrs in future. So I was subtracting that particular date yyyy-mm-dd from current date.

But as this did not work, i am now using a function timestampdiff

SET :DIFF-IN-MONTHS = TIMESTAMPDIFF(64,CHAR(
TIMESTAMP(DATE(:date-to-be-checked),'00.00.00') -
TIMESTAMP(DATE(:current-date),'00.00.00')))

Here 64 stands for months.

And my program now checks whether DIFF-IN-MONTHS >24(months) and hence the problem is now solved.
Back to top
View user's profile Send private message
Terry Heinze

JCL Moderator


Joined: 14 Jul 2008
Posts: 1249
Location: Richfield, MN, USA

PostPosted: Wed Apr 08, 2009 9:40 pm
Reply with quote

Thanks for getting back with your solution.
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 Timestamp difference and its average ... DB2 11
No new posts Difference when accessing dataset in ... JCL & VSAM 7
No new posts Need to fetch data from so many DB2 t... DB2 9
No new posts Amount of days between two dates PL/I & Assembler 8
No new posts What is the difference between Taskty... Compuware & Other Tools 2
Search our Forums:

Back to Top