MVSFORUMS.com Forum Index MVSFORUMS.com
A Community of and for MVS Professionals
 
 FAQFAQ   SearchSearch   Quick Manuals   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Calculate the difference between 2 dates in years

 
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database
View previous topic :: View next topic  
Author Message
omswarup
Beginner


Joined: 12 Dec 2006
Posts: 6
Topics: 2
Location: Bangalore

PostPosted: Mon Jun 14, 2010 10:50 pm    Post subject: Calculate the difference between 2 dates in years Reply with quote

I have a situation in which i have a column in a Db2 tabel which gives me Date of Birth, is there anyways of writing a query which would get me the details of the person whose age is greater than 18 years as of the date on which the query is being run.

There is no other field i can check on apart form the date of birth field on it.

Instead of writing a program i thought if there is a way of writing a query which can get this detail, it would be faster.

The date format is in CCYY-MM-DD format

Please help
Back to top
View user's profile Send private message
papadi
Supermod


Joined: 20 Oct 2009
Posts: 594
Topics: 1

PostPosted: Mon Jun 14, 2010 11:08 pm    Post subject: Reply with quote

Look here:

http://publibz.boulder.ibm.com/cgi-bin/bookmgr_OS390/handheld/Connected/BOOKS/dsnsqj10/2.21.6.1

And bookmark this as you will use the SQL Reference often. . .

The flashlight/tubelight is the manual search feature. If you search for table contents you can get to the Table of Contents for the manual (something else good to bookmark. . .
_________________
All the best,

di
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12372
Topics: 75
Location: San Jose

PostPosted: Tue Jun 15, 2010 10:16 am    Post subject: Reply with quote

omswarup,

As papadi pointed you can use date arithmetic to get validate the age. The result of date arithmetic is DECIMAL(8,0) which number of years, months, and days between the two dates. Since you are interested only in the year portion divide the result by 10,000 and only take the integer portion and check if it is greater than 18 like shown below

Code:

SELECT *                                                     
  FROM TABLE
 WHERE INT(CURRENT DATE - Your db2 DATE col) / 10000 >= 18 
 ;                                                           


Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
NASCAR9
Intermediate


Joined: 08 Oct 2004
Posts: 274
Topics: 52
Location: California

PostPosted: Tue Jun 15, 2010 10:42 am    Post subject: Reply with quote

kolusu wrote:
omswarup,

As papadi pointed you can use date arithmetic to get validate the age. The result of date arithmetic is DECIMAL(8,0) which number of years, months, and days between the two dates. Since you are interested only in the year portion divide the result by 10,000 and only take the integer portion and check if it is greater than 18 like shown below

Code:

SELECT *                                                     
  FROM TABLE
 WHERE INT(CURRENT DATE - Your db2 DATE col) / 10000 >= 18 
 ;                                                           


Kolusu


This is the way I do it. Not sure if DB2 translates the code to the same as yours.

Code:

SELECT  *
  FROM your table                 
WHERE YEAR(CURRENT DATE - BIRTH_DATE ) >= 18
fetch first 10 rows only

_________________
Thanks,
NASCAR9
Back to top
View user's profile Send private message
kolusu
Site Admin
Site Admin


Joined: 26 Nov 2002
Posts: 12372
Topics: 75
Location: San Jose

PostPosted: Tue Jun 15, 2010 10:54 am    Post subject: Reply with quote

NASCAR9,

yep it works and actually is much cleaner than mine . Thanks for the tip

Kolusu
Back to top
View user's profile Send private message Send e-mail Visit poster's website
omswarup
Beginner


Joined: 12 Dec 2006
Posts: 6
Topics: 2
Location: Bangalore

PostPosted: Wed Jun 16, 2010 4:12 am    Post subject: Reply with quote

Thanks a lot Kolusu and papadi for the inputs.

Regards,
Omswarup
NASCAR9 wrote:
kolusu wrote:
omswarup,

As papadi pointed you can use date arithmetic to get validate the age. The result of date arithmetic is DECIMAL(8,0) which number of years, months, and days between the two dates. Since you are interested only in the year portion divide the result by 10,000 and only take the integer portion and check if it is greater than 18 like shown below

Code:

SELECT *                                                     
  FROM TABLE
 WHERE INT(CURRENT DATE - Your db2 DATE col) / 10000 >= 18 
 ;                                                           


Kolusu


This is the way I do it. Not sure if DB2 translates the code to the same as yours.

Code:

SELECT  *
  FROM your table                 
WHERE YEAR(CURRENT DATE - BIRTH_DATE ) >= 18
fetch first 10 rows only
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic   printer-friendly view    MVSFORUMS.com Forum Index -> Database All times are GMT - 5 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


MVSFORUMS
Powered by phpBB © 2001, 2005 phpBB Group