Combination View Flat View Tree View
Threads [ Previous | Next ]
Age from Date of birth SQL
toggle
Age from Date of birth SQL
7/22/10 7:46 AM
Hi,
I recently had to try and roughly work out the age of a borrower based on their date of birth in msquery to export into a pivot table.

This is my solution.

 1
 2Age=(case
 3length(char(date_trunc('day','today')-borrower_main.birth,10))
 4when 10 then
 5int(left(char(date_trunc('day','today')-borrower_main.birth,10),5)/365.25)
 6when 9 then
 7int(left(char(date_trunc('day','today')-borrower_main.birth,10),4)/365.25)
 8when 8 then
 9int(left(char(date_trunc('day','today')-borrower_main.birth,10),3)/365.25)
10else ''
11end),


It's not perfect as leap years can cause it to be out by a year.
Does anyone else have a better way of doing this?

Steve
Flag Flag
RE: Age from Date of birth SQL
age date birth
9/24/10 8:34 AM as a reply to Steven Bannister.
Hi,

The following SQL will give you the borrower's age as a floating point number:

select interval('year', date('today') - birth) from borrower_main

for a borrower with a birth date of e.g. 14/04/87 this will return 23.447

if you just want a whole number of years then wrap the above in an 'int4()' conversion call:

select int4(interval('year', date('today') - birth)) from borrower_main

will return 23

Simon
Flag Flag
Calendar Calendar

Thursday

17

May 18, 2012
S M T W T F S
2930 1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31 12
Time Title Type  
There are no events on this day.
Showing 0 results.