Age Calculation Query [duplicate] - sql

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to calculate age in T-SQL with years, months, and days
calculating age from sysdate and birthdate using SQL Server
In the process of some age calculations. I'm a bit of a novice when it comes to SQL but I've been tasked with singling out persons who will turn 65yrs old as of 31 March 2013. So far I'm able to calculate age based on given DOB, however I'm having issues singling out these persons. I'm thinking this is a simple issue but I'm stumped, see sql statement. Can someone please point me in the right direction.
SELECT ip_master.ssn, ip_master.firstname, ip_master.surname, ip_master.status,
CASE WHEN DATEADD(YEAR, DATEDIFF (YEAR, dob, '2013/03/31'), dob) > '2013/03/31'
THEN DATEDIFF(YEAR, dob, '2013/03/31') - 1
ELSE DATEDIFF(YEAR, dob, '2013/03/31')
END AS 'Age'
FROM test_db.dbo.ip_master ip_master

Append a WHERE clause to your statement:
...
WHERE dob = '1948-03-31'

Would the following do? I just buried your query inside another query and applied restriction on age.
select * from
(
SELECT ip_master.ssn, ip_master.firstname, ip_master.surname, ip_master.status,
CASE WHEN DATEADD(YEAR, DATEDIFF (YEAR, dob, '2013/03/31'), dob) > '2013/03/31'
THEN DATEDIFF(YEAR, dob, '2013/03/31') - 1
ELSE DATEDIFF(YEAR, dob, '2013/03/31')
END AS 'Age'
FROM test_db.dbo.ip_master ip_master
)
where Age >= 65

Try the Below Query... it will work perfectly...
DECLARE #CalDate datetime;
DECLARE #CurDate datetime;
Declare #count INT
SET #CalDate ='03/31/2013'
SET #count =0
SELECT #CurDate = DateAdd(Month, 1, #CalDate)
SET #count = #count + datepart(dd,dateadd(dd,-1,dateadd(mm,1,cast(cast(year(#CurDate) as varchar)+'-'+cast(month(#CurDate) as varchar)+'-01' as datetime))))
SELECT ip_master.ssn, ip_master.firstname, ip_master.surname, ip_master.status,DATEDIFF (yy, DOB, cast(#CalDate as DateTime) + #count) - (
CASE SIGN (MONTH (DOB) - MONTH (cast(#CalDate as DateTime) + #count))
WHEN 1 THEN
1
WHEN -1 THEN
0
WHEN 0 THEN
CASE SIGN (DAY (DOB) - DAY (#CalDate + #count))
WHEN 1 THEN
1
WHEN 0 THEN
1
ELSE
0
END
END) as AGE into #temp
FROM test_db.dbo.ip_master ip_master
select * from #temp where AGE >= 65

Whatever you have done is correct.
For singling out you need a where clause.
SELECT ip_master.ssn, ip_master.firstname, ip_master.surname, ip_master.status,
CASE WHEN DATEADD(YEAR, DATEDIFF (YEAR, dob, '2013/03/31'), dob) > '2013/03/31'
THEN DATEDIFF(YEAR, dob, '2013/03/31') - 1
ELSE DATEDIFF(YEAR, dob, '2013/03/31')
END AS 'Age'
FROM test_db.dbo.ip_master ip_master
WHERE dob = '1948-03-31'

Related

SQL to calculate age based on year and month

I want to get a list of names who are turning 14 year old and 11 months. Can someone to help me fix the SQL below? Thanks!
SELECT NAME
FROM TABLE
WHERE
(cast(datediff(DAY, DOB, getDate() -1) /(365.23076923074) as int)>=14
AND cast(datediff(MONTH, DOB, getDate() -1) % (12) as int)>=11)
OR
(cast(datediff(DAY, DOB, getDate() -1) /(365.23076923074) as int)<=15
AND cast(datediff(MONTH, DOB, getDate() -1) % (12) as int)<=0)
These people turn 14, 11 months, today.
select name
from table
where DOB = cast(dateadd(mm,-11,dateadd(yy,-14,getdate())) as date)
--this is to eliminate the time on getdate
I am not sure if you need <= or just <. Please make changes accordingly.
select NAME from table Where DOB < = DATEADD(MONTH, -179, GETDATE())
Or If you are just looking for results between a span of one month then:
select NAME from table Where DOB BETWEEN DATEADD(MONTH, -179, GETDATE()) AND DATEADD(MONTH, -180, GETDATE())

How to calculate age (in years) based on Date of Birth in SQL

I want to get the 1st of the month following the date the person turns 70. How to achive this in SQL
i am calculating age using the formula
DECLARE #Date_of_birth DATETIME = '1915-10-02 00:00:00.000'
DECLARE #AGE INT
SELECT #AGE = FLOOR((CAST (GETDATE() AS INTEGER) - CAST(#Date_of_birth AS INTEGER)) / 365.25)
IF(#AGE > 70)
How to find the first of the month following the date ??
IF (#AGE >80)
You can use datediff to calculate their age, and then date add to find their 70th birthday. To find the first of the month afterwards, you can use the Month and Year functions.
create table #people (name varchar(30), birthdate date)
insert into #people
values ('Bob', '07/08/1976'), ('Tasha','05/30/1996'),('April','04/01/1971')
--This will give you everyone's age
select DATEDIFF(YY,birthdate,GETDATE()) as age
from #people
--This will give you the first month following the date that they turn 70
select Name, DATEADD(yy,70,birthdate) as [70thBday], convert(varchar,month(dateadd(m,1,DATEADD(yy,70,birthdate)))) + '/01/' + convert(varchar,YEAR(dateadd(m,1,DATEADD(yy,70,birthdate))))
from #people
declare #dob datetime = '1954-06-08'
declare #age int = 70
select DATEADD(m, DATEDIFF(m, -1, DATEADD(yy, #age, #dob)), 0)
you can use dateadd function like this
select dateadd(d,1 - datepart(d,dateadd(m,1,dateadd(yy,70",,#Date_of_birth)),dateadd(m,1,dateadd(yy,70",,#Date_of_birth)))
You can break down this query like this
declare #BD_70 date = dateadd(yy,70",,#Date_of_birth)
declare #Nxt_Month_70 = dateadd(m,1,#BD_70 date)
declare #First_Of_Month_70 = dateadd(d,1 - datepart(d,#Nxt_Month_70),#Nxt_Month_70)
Basically, you add 70 year, find next month and replace the day to the first of the month.
Here is how you would calculate age base on current date.
select case
when cast(getdate() as date) = cast(dateadd(year, (datediff(year, '1996-09-09', getdate())), '1996-09-09') as date)
then dateDiff(yyyy,'1996-09-09',dateadd(year, 0, getdate()))
else dateDiff(yyyy,'1996-09-09',dateadd(year, -1, getdate()))
end as MemberAge
go
This is how I calculate the age:
SELECT DATEDIFF(yy, [DateOfBirth], GETDATE()) + (CASE WHEN DATEPART(MONTH, GETDATE()) - DATEPART(MONTH, [DateOfBirth]) < 0 THEN -1 ELSE 0 END ) AS Age FROM [User]
You can try TIMESTAMPDIFF function.(Support MySQL, Apache Doris)
Example:
select TIMESTAMPDIFF(year,'1996-09-09',now()) as age;

Function that calculates age in SQL

I'm looking for a way to calculate the age of a person. I'm making a function for it that I can use later as well as this time.
Function should take a ID-Number which are 11 characters in sweden format which is 560404-1234.
And return the age of the person just the years. So far I've thought of making a subtraction with example: Getdate year 2014 - 19+IDNr datepart yy to get the sum.
Still stuck though.
Cheers
I am guessing that the Swedish id number format is YYMMDD followed by something else.
If so, then the following should work to get the date:
select cast('19'+left(idn, 6) as date)
Then the age would be:
select datediff(year, cast('19'+left(idn, 6) as date), getdate())
What do the Swedes do about people born in the last 14 or so years? What happens to those who are over 100 years old?
If "dean" is correct, then you can try following code,
Create FUNCTION [dbo].[udfn_Get_Age_In_Years]
(
#ID_Number varchar(50)
)
Returns int
--select dbo.[udfn_Get_Age_In_Years]('560404-1234')
as
begin
Declare #retVal int = 0
select #retVal = Datediff(mm,col_dob,GETDATE()) / 12 from myTable
where ID_Number = #ID_Number
Return #retVal
end
This is my answer it's similar to the above ones but this is how I've solved it after a while.
ALTER function [dbo].[Function_AGE]
(
#IdNr varchar(13)
)
returns int
as
begin
Declare #Calc int
Set #IdNr = (Select Case when LEN(#IdNr) > 11
then cast(left(#IdNr, 8) as date)
else cast('19'+left(#IdNr,6) as date) end)
set #Calc = (select datediff(year,#IdNr,getdate()))
Return #Calc
end
Here is how you would calculate age of a person, given their age and current date
select case
when cast(getdate() as date) = cast(dateadd(year, (datediff(year, '1996-09-09', getdate())), '1996-09-09') as date)
then dateDiff(yyyy,'1996-09-09',dateadd(year, 0, getdate()))
else dateDiff(yyyy,'1996-09-09',dateadd(year, -1, getdate()))
end as MemberAge
go
To Compare a birthdate to #Date, the DATEDIFF gets you close. Subtract off of that if the month and day haven't occurred yet in the year.
CASE WHEN DATEPART(mm,birthdate) > DATEPART(mm,#Date)
THEN DATEDIFF(YEAR, birthdate AS DATE), #Date) - 1
WHEN DATEPART(mm,birthdate) = DATEPART(mm,#Date)
AND DATEPART(dd,birthdate) > DATEPART(dd,#Date)
THEN DATEDIFF(YEAR, birthdate AS DATE), #Date) - 1
ELSE DATEDIFF(YEAR, CAST(birthdate AS DATE), #Date)
END AS Age

Count the amount of years from a certain date SQL

I want to count the amount of years from a the year the person is born to today. I have the components to use but dont know how to use them.
SELECT COLUMN1, COLUMN2, DATEPART(YY,GETDATE()),
CONVERT(INT,+19)))LEFT(TABLE.COLUMN,2)
I want to use the +19 to show it before the birtyear. Example in the database the birthyear is showed as YY not YYYY. That is why I want to add the 19, so the SQL will count years from 19YY to 2013.
Try this
SELECT id
, Name
, DATEDIFF(yy, CONVERT(DATETIME, DOB), GETDATE()) AS AGE
, DOB
FROM MyTable
declare #DOB date = '19680411'
select datediff(year, #DOB, getdate())- case when month(#DOB)*32 + day(#DOB) >
month(getdate()) * 32 + day(getdate()) then 1 else 0 end

How to calculate Age/Number of Years between two dates [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
On a project I was working on, I was required to calculate a persons age when they join the system; after looking on the internet I found various ways this could be done, but most of them had slight issues when it involved a Leap-Year.
The solution below is how I calculate number of years past / age. Hope this helps others
You need to add the following method to your database:
CREATE FUNCTION [dbo].[fnCalAge] (#DiffFrom DATE, #DiffTo DATE) RETURNS INT
AS
BEGIN
DECLARE #NumOfYears INT
SET #NumOfYears = (SELECT
DATEDIFF(YEAR, #DiffFrom, #DiffTo) +
CASE
WHEN MONTH(#DiffTo) < MONTH(#DiffFrom) THEN -1
WHEN MONTH(#DiffTo) > MONTH(#DiffFrom) THEN 0
ELSE
CASE WHEN DAY(#DiffTo) < DAY(#DiffFrom) THEN -1 ELSE 0 END
END)
IF #NumOfYears < 0
BEGIN
SET #NumOfYears = 0;
END
RETURN #NumOfYears;
END
You then call it in your SQL Query, similar to the following:
SET DATEFORMAT dmy
SELECT dbo.fnCalAge(CAST('20/06/1987' AS DATE), CAST('20/06/2013' AS DATE))
assuming #bDate is datetime of birthdate and #today is todays date, then...
Declare #bDay Date = '31 dec 2000'
Declare #today Date = cast(getdate() as date)
Select datediff(Year, #bDay, #today) -
case When datepart(dayofYear, #today) <
datepart(dayofYear, #bDay) Then 1 Else 0 End
Replace hiredate with DOB for age. Replace sysdate with your date such as to_date('28-DEC-2012') :
SELECT empno, ename, hiredate, TRUNC(MONTHS_BETWEEN(sysdate, hiredate)/12) years_of_service
FROM scott.emp
/