What is the best way to calculate the age in years from a birth date in sqlite3?
相关问题
- SQL/SQL-LITE - Counting records after filtering
- Date with SimpleDateFormat in Java
- What SQLite NuGet package do I need
- How to write the array into the sqlite database fo
- VBA local timezone adjustment
To get @Bruno Costa's answer to work I had to add brackets around the strftime difference calculation on the 4th line and reverse the comparison to 'now' to be less than or equal: as I understand it the calculation is working out whether the calculated date is before or after today. If the calculated date is today or earlier then the birthday has already been this year or is today:
Note that the solution linked to in @Bruno Costa's answer was untested.
You can convert the dates to floating point numbers and subtract…
…where
dob
is a valid SQLite time string.Another solution is to use the day of the year
%j
as a fraction of the year by dividing it by 365.0. So you end up with:You can then cast the result to an integer:
or use ROUND() to round the outcome:
I have updated the calculation so it uses the correct decimal number of days in a solar year as noted by Robert in the comment below.
The difference with the other calculations is that the result of this calculation is a decimal number that can be cast to an integer (to get the exact amount of years) or rounded (to get the approximate amount of years).
For birthdays the approximate amount of years may not be that relevant, but for the age of something you bought it makes more sense.
IT is very dificult to do in sqllite... so better you retrive the birth date from the database nd then add this logic
The first function will calculate the birth date in calendar format, second will calculate current date, the third will find the difference between the two. These functions shud be called as shown below
dateOfBirth = mConvert_Date_To_Calendar(age);
currentDate = myCurrentCalender();
candidateAge = daysBetween(dateOfBirth.getTime(), currentDate.getTime());
"candidateAge" will have the age.
I found this that maybe would help you:
From http://www.mail-archive.com/sqlite-users@sqlite.org/msg20525.html
Just to clarify kai's answer (it seems that editing is now very much unwelcome, and comments have a strong size limitation and formatting issues):
Let's assume we want full years: for the first calendar year of life, this would be zero.
For the second calendar year, this would depend on the date -- i.e. it would be:
strftime('%m-%d', 'now') >= strftime('%m-%d', Birth_Date)
["case A"]( I assume sqlite does lexicographic comparison of two date strings and returns an integer value. )
For any other calendar year, it would be the number of years between the current and the second -- i.e.
strftime('%Y', 'now') - strftime('%Y', Birth_Date) - 1
, plus the same as above.In other terms, we subtract 1 from
strftime('%Y', 'now') - strftime('%Y', Birth_Date)
only if the opposite of "case A" happens -- that is, if and only ifstrftime('%m-%d', 'now') < strftime('%m-%d', Birth_Date)
, hence the formula.