How to calculate date based on yyyy-mm-dd - sql

I am looking for a simple query to calculate someones age based on yyyymmdd. I don't need to keep the records updated throughout time, I just need the age as of today.
Example - 19731026 is the birthdate for one record. I want to create a new column named age and calculate that birthdate to their current age which would be 48 years old.

If your database is Oracle you can use:
select floor(months_between(sysdate, to_date('19731026','YYYYMMDD'))/12) as age
from dual;
AGE
----------
48

Related

Calculating age at time of an event in SQL [duplicate]

This question already has answers here:
How to calculate age (in years) based on Date of Birth and getDate()
(40 answers)
Closed 2 years ago.
What is the best way to calculate age based on an event? I'm trying to look for individuals based on their age when an event took place? I'm relative new to SQL and the dates functions, I can't wrap my head around.
Example:
ID Name Age DOB AdmitDate
1234 John Smith 61 1960-07-15 2019-10-22
How can I can calculate whose age was 59 at the time of admit date?
DATEDIFF provides the function to do this.
e.g.
SELECT DATEDIFF ( yy , '1960-07-15' , '2019-10-22' )
Select * from yourtable
where DATEDIFF( year , CAST(DOB AS datetime), CAST(AdmitDate AS datetime)) >= 59
Try this

Rounding dates in SQL

I'd like to figure out the age of a person based on two dates: their birthday and the date they were created in a database.
The age is being calculated in days instead of years, though. Here's my query:
SELECT date_of_birth as birthday, created_at, (created_at - date_of_birth) as Age
FROM public.users
WHERE date_of_birth IS NOT NULL
The date_of_birth field is a date w/o a timestamp, but the created_at field is a date with a timestamp (e.g. 2017-05-06 01:27:40).
And my output looks like this:
0 years 0 mons 9645 days 1 hours 27 mins 40.86485 secs
Any idea how can I round/calculate the ages by the nearest year?
Using PostgreSQL.
If you are using MS SQLServer than you could
CONVERT(DATE, created_at)
and than calculate difference in months like
DATEDIFF(month, created_at, GETDATE())/12
means you can use reminder in months to add or substract one year.
In PostgreSQL, dates are handled very differently to MSSQL & MySQL. In fact it follows the SQL standard very well, even if it’s not always intuitive.
To actually calculate the age of something, you can use age():
SELECT age(date1,date1)
Like all of PostgreSQL’s functions, there are variations of data type, and you may need to do something like this:
SELECT age(date1::date,date1::date)
or, more formally:
SELECT age(cast(date1 as date),cast(date1 as date))
The result will be an interval, which displays as a string :
SELECT age(current_date::date,'1981-01-17'::date);
-- 36 years 3 mons 22 days
If you just want the age in years, you can use extract:
SELECT extract('year' from age(current_date::date,'1981-01-17'::date));
Finally, if you want it correct to the nearest year, you can apply the old trick of adding half an interval:
extract('year' from age(current_date::date,'1981-01-17'::date)+interval '.5 year');
It’s not as simple as some of the other DBMS products, but it’s much more flexible, if you can get your head around it.
Here are some references:
https://www.postgresql.org/docs/current/static/functions-datetime.html
http://www.sqlines.com/postgresql/how-to/datediff

Update age column by one year

How to update the age column by only one year for each row in the table,
so how to get the year of that column and increase it by 1.
My data looks like:
ID name age
1 sarah 1992-05-26 00:00:00
2 suzan 1991-05-20 00:00:00
For your specific question you can add one year onto each date with a simple DateAdd:
UPDATE DataTable SET Age = DATEADD(year, 1, Age)
However, I would store the Date of Birth in the database and calculate the Age in the Business Layer or wherever it needs to be displayed.
For reference, if you do store the Date of Birth, then the age can be calculated with a simple query using a DateDiff function to calculate the number of whole years between the date of birth and todays date:
SELECT DT.Name
, DT.DateOfBirth
, DATEDIFF(year, DT.DateOfBirth, GETDATE()) As Age
FROM DataTable DT
your query should look like this:
Update yourTable set age = DATEADD(yy,1,age)
this will increase year by 1

checking age of a person, the person has date of birth stored as a date

select
//
from
//
where
//this is the place i need help with
I have a table person with column dob date.
I want to be able to select rows of people aged 1 or more.
some mock date:
name dob
person 1 13-DEC-2014
person 2 24-JAN-2011
person 3 05-MAY-2013
person 4 17-APR-2014
person 5 21-DEC-2013
person 6 11-NOV-2014
in this scenario i would expect the names 'person 2', 'person 3' and 'person 5' to be listed in the output. i know how to do the select and from statement in my scenario, just not a where. any help would be greatly appreciated.
select *
from person
where dob <= add_months( trunc(sysdate), -12 )
will return everyone whose birth date is more than 12 months before the current date. sysdate returns the current date and time. trunc removes the time component (setting it to midnight). Then add_months subtracts 12 months.
I would use ADD_MONTHS():
SELECT * FROM person
WHERE dob <= TRUNC(ADD_MONTHS(SYSDATE, -12));
In the above I'm actually adding -12 months (or 1 year -- equivalent to subtracting 12 months) to the value of SYSDATE.
Calculate the threshold and compare dob to that:
where dob <= add_months(sysdate, -12))
Using a constant expression for the threshold would also mean that if an index existed on dob then it would be a candidate for usage. Even if an index didn't exist, it would still be much more efficient than ccukating a date from (every value of) dob and comparing it to today.
to_char(sysdate,'YYYY') - to_char(dob,'YYYY')>1

Find year of birth from given age in sql

can somebody help me with this problem, I know I must use sysdate. For example I have entity EMPLOYEE with ATRIBUTES Emp.ID and Age.
If we supply ADD_MONTHS with a negative number it subtracts that many months from the given date. Multiplying the AGE by -12 gives us the number of months we need to subtract from the current date to derive the approximate birthday.
SELECT Emp.ID
, TO_CHAR(
ADD_MONTHS(sysdate, (Emp.Age*-12))
, 'YYYY') as year_of_birth
FROM employee Emp;
This will not be accurate as exact month is not known
SELECT id, Name, TRUNC(sysdate - age*365) as DOB FROM Employee