views:

42

answers:

3

What is the best way to calculate the age in years from a birth date in sqlite3?

A: 

There isn't a simple way to do this with SQLite out of the box.

As an alternative, you can roll your own functions that can be called from SQLite.

Marcelo Cantos
A: 

I found this that maybe would help you:

select
    case
        when date(dob, '+' ||
            strftime('%Y', 'now') - strftime('%Y', dob) ||
            ' years') >= date('now')
        then strftime('%Y', 'now') - strftime('%Y', dob)
        else strftime('%Y', 'now') - strftime('%Y', dob) - 1
    end
    as age
from t;

From http://www.mail-archive.com/[email protected]/msg20525.html

Bruno Costa
+1  A: 
SELECT (strftime('%Y', 'now') - strftime('%Y', Birth_Date)) - (strftime('%m-%d', 'now') < strftime('%m-%d', Birth_Date));

This one works.
I merely ported this MySQL example: http://dev.mysql.com/doc/refman/5.0/en/date-calculations.html

kai