Calculating Age Using DoB from a Specific Date in the past
Your calculations are wrong. That person should be 28 at that time.
A simple trick is to treat the date as a number in the form of yyyyMMdd and then subtract the target date from DOB and get the integer part. With your sample dates:
DECLARE @dob DATETIME='19910218';
DECLARE @targets TABLE(target DATETIME);
INSERT @targets(target)VALUES('20200201'), (GETDATE());
SELECT
FLOOR(((YEAR(target)* 10000+MONTH(target)* 100+DAY(target))
-(YEAR(@dob)* 10000+MONTH(@dob)* 100+DAY(@dob))
)/ 10000
)
FROM @targets;
PS: Floor might be unnecessary, SQL server makes an integer division but I would like to keep it as a safety.
Oracle Age calculation from Date of birth and Today
SQL> select trunc(months_between(sysdate,dob)/12) year,
2 trunc(mod(months_between(sysdate,dob),12)) month,
3 trunc(sysdate-add_months(dob,trunc(months_between(sysdate,dob)/12)*12+trunc(mod(months_between(sysdate,dob),12)))) day
4 from (Select to_date('15122000','DDMMYYYY') dob from dual);
YEAR MONTH DAY
---------- ---------- ----------
9 5 26
SQL>
How to get an age from a D.O.B field in MySQL?
A few ways:
select DATEDIFF(customer.dob, '2010-01-01') / 365.25 as age
SELECT DATE_FORMAT(FROM_DAYS(DATEDIFF(customer.dob,'2010-01-01')), ‘%Y')+0 AS age
Hope this helps you
Calculate age from BirthDate
Here is a Java method called getAge which takes integers for year month and day and returns a String type which holds an integer that represents age in years.
private String getAge(int year, int month, int day){
Calendar dob = Calendar.getInstance();
Calendar today = Calendar.getInstance();
dob.set(year, month, day);
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)){
age--;
}
Integer ageInt = new Integer(age);
String ageS = ageInt.toString();
return ageS;
}
Calculate age based on date of birth
PHP >= 5.3.0
# object oriented
$from = new DateTime('1970-02-01');
$to = new DateTime('today');
echo $from->diff($to)->y;
# procedural
echo date_diff(date_create('1970-02-01'), date_create('today'))->y;
demo
functions: date_create()
, date_diff()
MySQL >= 5.0.0
SELECT TIMESTAMPDIFF(YEAR, '1970-02-01', CURDATE()) AS age
demo
functions: TIMESTAMPDIFF()
, CURDATE()
Related Topics
Sql Call Stored Procedure for Each Row Without Using a Cursor
Pg Copy Error: Invalid Input Syntax for Integer
Mysql Query - Records Between Today and Last 30 Days
How to Escape Back Slash in SQL Server
How Two Join to Tables Based on the Highest Value in One of Them
Select Query to Remove Non-Numeric Characters
Oracle Pl/Sql String Compare Issue
Concat(Column) Over(Partition By...)? Group-Concatentating Rows Without Grouping the Result Itself
Comparing Two Columns Using SQL to Make Sure the Same Set of Values Are Present in Both
Sql Query Is Taking More Than 4 Minutes
Mysql Error 1264: Out of Range Value for Column
Mysql Left Join With Limit 1 Not Returning Desired Result
How to Calculate Difference of Values in a Successive Rows
Sql Server Count Instances of Most Frequent Data
Postgresql Error: Syntax Error At or Near "Varchar"
How to Find Multiple Occurrence of Particular String and Fetch Value in SQL Server
How to Select the Last Record from MySQL Table Using SQL Syntax