View previous topic :: View next topic
|
Author |
Message |
Debasis Misra Warnings : 1 New User
Joined: 16 Sep 2008 Posts: 72 Location: Bangalore
|
|
|
|
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 |
|
|
kregen
New User
Joined: 16 Mar 2006 Posts: 21
|
|
|
|
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 |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Notice that the original sql used "year" and the suggestion uses "date".
Quite a difference. . . . |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10873 Location: italy
|
|
|
|
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 |
|
|
Debasis Misra Warnings : 1 New User
Joined: 16 Sep 2008 Posts: 72 Location: Bangalore
|
|
|
|
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 |
|
|
Terry Heinze
JCL Moderator
Joined: 14 Jul 2008 Posts: 1249 Location: Richfield, MN, USA
|
|
|
|
Thanks for getting back with your solution. |
|
Back to top |
|
|
|