Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calculate Age in SQL 1

Status
Not open for further replies.

cantona

Technical User
May 20, 2001
121
GB
I have the following select statement in Access that i need to convert to a pass through query. How would i convert to oracle sql?

Int((Int("20" & Left([YEAR_CODE],2) & "0831")-Val(Format([DATE_OF_BIRTH],"yyyymmdd")))/10000) AS Age

The year code field is stored as four digits 0099, 0607 etc..

 
How does the code in YEAR_CODE relate to an actual year? Do you want age according to the current date or according to YEAR_CODE in the database?
 
Hi,

I need it calculating by the year code. So for year code 0607, it would be based on 06.

Thanks
 
Sorry, didnt explain myself very well! Its an academic year code so 0607 would mean 2006/2007. The Access formula is cutting off the 07 to leave 06, meaning 2006. Does that help?
 
Ok, assuming 0099 should be 9900 and DATE_OF_BIRTH is already a DATE column....
Code:
SELECT Floor(Months_Between(To_Date('3108' || Substr(YEAR_CODE,1,2),'DDMMRR'),DATE_OF_BIRTH)/12)
FROM   sys.dual
 
Thanks alot for your help! Thats exactly what i was looking for! Im relatively new to working with Oracle SQL, Could you perhaps explain what each part of the SQL is doing to better help me understand?

Many thanks
 
[tt]Substr(year_code,1,2)[/tt]: Take two characters of year_code, starting at character 1.

[tt]To_Date('<string>','DDMMRR')[/tt]: Convert string to date, assuming string is in DDMMYY format. RR converts two digit year to a year between 1951 and 2050.

[tt]Months_Between(<date>,<date>)[/tt]: Number of months between two dates. Earliest date first yeilds negative number.

[tt]Floor(<number>)[/tt]: Same as INT. Number rounded down to nearest full number.
 
Oh, and || means concatenate two strings together to form one string.
 
Thanks, Thats extremely helpful! This should help me out no end in the future!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top