INTELLIGENT WORK FORUMS FOR COMPUTER PROFESSIONALS
Come Join Us!
- Talk With Other Members
- Be Notified Of Responses
To Your Posts
- Keyword Search
- One-Click Access To Your
Favorite Forums
- Automated Signatures
On Your Posts
- Best Of All, It's Free!
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.
Partner With Us!
"Best Of Breed" Forums Add Stickiness To Your Site

(Download This Button Today!)
Member Feedback
"...I wish I knew about this site years ago. It would have saved me a lot of heartaches..."
Geography
Where in the world do Tek-Tips members come from?
|
Calculate Age in SQL
|
|
|
cantona (TechnicalUser) |
14 Mar 07 4:56 |
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..
|
|
lewisp (Programmer) |
14 Mar 07 6:09 |
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? |
|
|
cantona (TechnicalUser) |
14 Mar 07 8:04 |
Hi,
I need it calculating by the year code. So for year code 0607, it would be based on 06.
Thanks |
|
lewisp (Programmer) |
14 Mar 07 8:16 |
So what does 0099 and 0607 actually mean? |
|
|
cantona (TechnicalUser) |
14 Mar 07 8:21 |
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? |
|
lewisp (Programmer) |
14 Mar 07 9:13 |
Ok, assuming 0099 should be 9900 and DATE_OF_BIRTH is already a DATE column.... CODESELECT Floor(Months_Between(To_Date('3108' || Substr(YEAR_CODE,1,2),'DDMMRR'),DATE_OF_BIRTH)/12) FROM sys.dual |
|
|
cantona (TechnicalUser) |
14 Mar 07 10:48 |
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 |
|
lewisp (Programmer) |
14 Mar 07 10:55 |
Substr(year_code,1,2): Take two characters of year_code, starting at character 1.
To_Date('<string>','DDMMRR'): Convert string to date, assuming string is in DDMMYY format. RR converts two digit year to a year between 1951 and 2050.
Months_Between(<date>,<date>): Number of months between two dates. Earliest date first yeilds negative number.
Floor(<number>): Same as INT. Number rounded down to nearest full number. |
|
lewisp (Programmer) |
14 Mar 07 10:56 |
Oh, and || means concatenate two strings together to form one string. |
|
|
cantona (TechnicalUser) |
14 Mar 07 11:03 |
Thanks, Thats extremely helpful! This should help me out no end in the future! |
|
|
 |
|