get age in years for month of july [duplicate] - sql

This question already has answers here:
How to calculate age (in years) based on Date of Birth and getDate()
(40 answers)
Closed 3 months ago.
I have a table that stores the date of birth of patients in DateTime format(2022-06-22).
How can I get the age in years for people born in July?
I have tried the below:
select sFirstName,
sLastName,
dDateOfBirth,
DATEDIFF(yy,dDateOfBirth,GETDATE()) as Age,
ifkMedicalAidID
from Patients
where DATEDIFF(month,dDateOfBirth,GETDATE()) between 5 and 5
Although I get only all the records for July(which is correct) all the ages are 0, I see the problem with the above query but I don't have the SQL knowledge to resolve it...

SELECT
FLOOR((DATEDIFF(DAY,0,GETDATE()) - DATEDIFF(DAY,0,dDateOfBirth)) / 365.2425) AS AgeYears

this did the trick thanks John
select sFirstName,
sLastName,
dDateOfBirth,
DATEDIFF(yy,dDateOfBirth,GETDATE()) as Age,
ifkMedicalAidID
from Patients
where datepart(month,dDateOfBirth)=7

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

Select all participants under the age of 18 using the current date in PostgreSQL [duplicate]

This question already has answers here:
PostgreSQL Age Calculation from date type
(2 answers)
Closed 3 years ago.
I want to select all participants who are not 18 years old, but using the current date how do I do this.
I would not like to use the following way,
SELECT * FROM participants WHERE data_of_birth < '2020-01-01';
Does anyone know otherwise?
It can be solved with an interval:
SELECT * FROM participants WHERE data_of_birth > (current_date - '18 years'::interval)::date;

need sql query to get min and max experience?

I have a table with 2 columns (exp_Years, exp_months).
Saving years and months in 2 different columns.
Now I need a SQL Query to get min and max experience
If minExp=2years and maxExp=4years
how do I get records with out getting 4.1 and 4.2 exp?
example data:--
table: Resume(name, email, phone, exp_years, exp_months)
I need list of records with exp_years>2 and exp_years<5... but how to check exp_months here..
if I use exp_years>2 and exp_years< 5 condition am getting records with 5years 1 month, 5year 2 months are also getting.
I need only more than 2 years and less than or equals 5 years 0 months.. but not 5years 1month and moreee....
actually I need hibernate HQL Query....
Thanks in advance.
This is a strange way of storing the data. So you are supposed to store two years and three months as years = 2 and months = 3.
But you could just as well store years = 1 and months = 15 or years = 0 and months = 27, which would be the same time span. This said, it would be better to have just one column, i.e months only.
Then to get records with a time span of over 2 years and until 5 years exactly you'd select
where months between 25 and 60
If you must live with the table design you are showing, then you can always calculate total months = years * 12 + months. That would be
where years * 12 + months between 25 and 60
you can make this by add months to years after divide months on 12 then you can use ROUND to round to int value:
select ROUND(max(exp_Years+exp_months/12)), ROUND(min(exp_Years+exp_months/12))
from your_table

SQL last month date of given date [duplicate]

This question already has answers here:
Get the last day of the month in SQL
(22 answers)
Closed 9 years ago.
i am using sql
How to get the last day of the month for a given date
01/03/2014 should return 01/31/2014
I only need the date part.. Thanks
For SQL-SERVER, try this:
SELECT convert(varchar,(DATEADD(s,-1,DATEADD(mm, DATEDIFF(m,0,'01/03/2014 ')+1,0))),101)

calculating age from sysdate and birthdate using SQL Server [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate age in T-SQL with years, months, and days
I'm simply trying to calc age (in years) using a birthdate variable
1932-04-29 00:00:00.000
and SYSDATETIME( ) in SQL Server, where by
SELECT year(Sysdatetime() - Birthdate) as Age
produces (surprisingly) : 1980
What did I miss? I expected to get 80!
Calculating age is not as simple as it might first appear.
If you use Datediff you get a difference in absolute years, which is not the age.
eg
select DATEDIFF(yy, '1980-12-31', getdate())
will return 32, whereas the age of the person in question is 31.
This might be accurate enough for your purposes.
More accurate, but still wrong, you can use
select convert(int,DATEDIFF(d, '1933-10-31', getdate())/365.25)
which is right most of the time.
Or you can write a more complex function....
CREATE Function [dbo].[F_GetAge]( #RefDate Datetime,#Birthdate Datetime) Returns Int as
/*
200040916 Thomas Wassermann
*/
Begin
Declare #Alter Int
if #RefDate>#Birthdate
Select #Alter=(DatePart(yy,CAst(#Refdate-#Birthdate -1 as Float))-1900)
else select #Alter=0
Return(#Alter)
end
Quick and dirty. Can have some errors bepending on leap years, etc.
-- quick and dirty age in years
declare #birthday datetime
set #birthday = '19320429'
-- this is pretty close, but could have problems with leap years, etc.
select floor(datediff(day, #birthday, CURRENT_TIMESTAMP) / 365.25)
What happens here is the following:
both dates get converted to floats as number of days since 1900-01-01T00:00:00
the floats get subtracted from each other yielding the number of days between them
the result gets converted back to a date by adding the number of days to the base date
To correctly calculate the difference use DATEDIFF: http://msdn.microsoft.com/en-us/library/ms189794.aspx
But there is still a problem with that. See here for a complete solution: How to calculate age (in years) based on Date of Birth and getDate()