Calculating age in a certain month in SQL Server - sql

I need to extraxt a list of users who turn 15 years old in any day in a certain month (e.g. in June or in July) and I am trying to use string comparison in SQL Server (stored procedure) but it is not working.
I get #Month parameter from a SSRS which is given from a dropdown list of next 10 months. (some of this month will be in the next year).
I have an Age function which converts a date format from 27/07/2003 (BirthDate) to a string 15 years,2 months,27 days. Naturally, There are people aged 9 years, 0 months, 2 days as well.
So far, I could write the code to check if the person will be 15 and at least 1 month (15 years,1 months) in next July (#Month + 1) supposing we want to know if they turn 15 in June (#Month) but still it is not working because of string comparison.
Age(BirthDate, GETDATE()) is the function which turns age in this format:
15 years,2 months,27 days as string.
I hope it is clear what I mean.
declare #Age varchar (20) = 15,
#Month varchar (25) = 'June',
#CurrentMonth varchar (20) = null,
SET #CurrentMonth = DATENAME(month, GETDATE()); /* returns current month in string */
SELECT My_ID
,Title
,FirstName
,LastName
,Gender
,CONVERT(VARCHAR, BirthDate, 103) AS BirthDate
,dbo.Age(BirthDate, GETDATE()) AS Age
,LocalityName AS Locality
,GETDATE() AS ReportDate
,MONTH(GETDATE()) AS MONTH
,YEAR(GETDATE()) AS YEAR
FROM dbo.vw_individuals
WHERE (LEFT(dbo.Age(BirthDate, DATEADD(month, 1 +
(SELECT DATEDIFF(MONTH, #CurrentMonth + ' 01 2010', #Month + ' 01 2010')
+ CASE WHEN DATEDIFF(MONTH, #CurrentMonth + ' 01 2010', #Month + ' 01 2010') < 0 THEN 12 ELSE 0 END)
, GETDATE())), 2) = #Age)
AND (LEFT(dbo.Age(BirthDate, DATEADD(month, 1 +
(SELECT DATEDIFF(MONTH, #CurrentMonth + ' 01 2010', #Month + ' 01 2010')
+ CASE WHEN DATEDIFF(MONTH, #CurrentMonth + ' 01 2010', #Month + ' 01 2010') < 0 THEN 12 ELSE 0 END)
, GETDATE())), 16) < #Age + ' years,2 month')
AND (month(convert(DATETIME, BirthDate, 103)) = (SELECT DATEPART(MM, #Month + '01 2010'))) /* Checks if the BirthDate month is the same as the chosen month (#Month) */

so given the #Month and #Year you're interested in, then the people you need are given BY
SELECT ..... WHERE YEAR(BirthDate) = #YEAR - 15 AND Month(BirthDate) = #Month
so all the people returned have a 15th birthday in #Month/#Year
-- bit of a pain though, might not quite work if a person is born 29th February, they turn 15 in March although that will never be a leap year, so you could get away with
SELECT ..... WHERE YEAR(BirthDate) = #YEAR - 15
AND Month(BirthDate)
+ CASE WHEN MONTH(Birthdate) = 2 AND DAY(Birthdate) = 29 THEN 1 ELSE 0 END
= #Month

Below should show you way to go, your query is much too complicated for what you need (I think), just use logic from my IF in your WHERE
declare #dob datetime = '2003-10-13 17:21:45.620'
if(month(#dob) = month(getdate())
AND year(#dob)+15 = year(getdate()))
print '15 years old this month'
else
print 'not 15 years old this month'
The same yoke, just used table variable:
declare #dob datetime = '2003-10-13 17:21:45.620'
declare #t table (dob datetime)
insert into #t
values ('2003-10-13 17:21:45.620'), ('2004-10-13 17:21:45.620'), ('2003-10-30 17:21:45.620')
select *
from #t
where month(dob) = month(getdate())
and year(dob)+15 = year(getdate())
EDIT
Other way to achieve what you need is to use DATEDIFF
declare #dob datetime = '2003-10-13 17:21:45.620'
declare #t table (dob datetime)
insert into #t
values ('2003-10-13 17:21:45.620'),
('2004-10-13 17:21:45.620'),
('2003-10-30 17:21:45.620')
select *
from #t
where datediff(month, dob, getdate()) = 180 --(15 years * 12months)

Related

How Calculate Expire Date in SQL Query

I have this query for expiry date calculation and its work fine but result show in - eg:-9 year 4 month and 5 day.
I want to show that in normal way like "Expire in 9 years 4 months and 5 day":
DECLARE #TempDate Datetime ,
#ExpiryDate Datetime,
#year int,
#month int,
#day int
SET #ExpiryDate = (SELECT TOP (1) [ExpiryDate] FROM [dbo].[Purchases] WHERE [ProductId] = 1)
SELECT #TempDate = #ExpiryDate
SELECT
#year = DATEDIFF(YEAR, #TempDate, GETDATE()) -
CASE
WHEN (MONTH(#ExpiryDate) > MONTH(GETDATE())) OR
(MONTH(#ExpiryDate) = MONTH(GETDATE()) AND DAY(#ExpiryDate) > DAY(GETDATE()))
THEN 1 ELSE 0
END
SELECT #TempDate = DATEADD(YEAR, #year, #TempDate)
SELECT #month = DATEDIFF(MONTH, #TempDate, GETDATE()) -
CASE
WHEN DAY(#ExpiryDate) > DAY(GETDATE())
THEN 1 ELSE 0
END
SELECT #TempDate = DATEADD(MONTH, #month, #TempDate)
SELECT #day = DATEDIFF(DAY, #TempDate, GETDATE())
SELECT #year AS Years, #month AS Months, #day AS [Days]
If I understand your question correctly, the calculation is working as you expect but you want the Years value to be returned as a positive rather than negative number. If this is the case, you should change the final SELECT to:
SELECT (#year * -1) AS Years, #month AS Months, #day AS [Days];
Alternatively if you want to return the output as a string (i.e. Expire in 9 years 4 months and 5 day), change the final SELECT to:
SELECT 'Expire in ' + CAST((#year * -1) AS VARCHAR(2)) + ' years '
+ CAST(#month AS VARCHAR(2)) + ' months and '
+ CAST(#day AS VARCHAR(2)) + ' day';
Casting as VARCHAR(2) assumes that you expect no more than 99 years, but you may want to increase that number.

How to get month (in string) diff in sql server

How can I get month diff as integer based on month names in string (January, February, March, etc.) ?
For example, I have a variable declare #month varchar(20) = 'May';
and Select DATEDIFF(month, month(getdate()), #month) should be 7 (the
difference between October and May).
In SQL Server, you would use datediff():
select datediff(month, '2010-01-15', '2011-02-28')
This counts the number of month boundaries between two dates. That sounds like a reasonable interpretation of your question.
Declare #M1 varchar(25) = 'October'
Declare #M2 varchar(25) = 'May'
Select DateDiff(MONTH,#M1+' 01 1980',#M2+' 01 1980')
+case when DateDiff(MONTH,#M1+' 01 1980',#M2+' 01 1980')<0 then 12 else 0 end
Returns 7
DECLARE #M1 as varchar(20) = 'October';
DECLARE #M2 as varchar(20) = 'May';
SELECT ABS(datediff(mm,
convert(datetime, #M1 + ' 1 2012 11:01AM', 100),
convert(datetime, #M2 + ' 1 2012 11:01AM', 100)
)
);
this doesn't allow for years ,which you didn't mention previously
Assuming input1,input2, there you go:
declare #input1 nvarchar(max)='October'
declare #input2 nvarchar(max)='May'
select
case
when DATEPART(MM,#input1+' 01 2011')>DATEPART(MM,#input2+' 01 2011')
then 12
else 0
end-DATEPART(MM,#input1+' 01 2011')+DATEPART(MM,#input2+' 01 2011')
try this
select DATEPART(MM,'march 01 2011') - DATEPART(MM,'january 01 2011')
Not sure what you are trying to do, but something like this might work:
select MONTH(STR_TO_DATE('October', '%M')) - MONTH(STR_TO_DATE('May', '%M')) -- returns 5
Try this
DECLARE #Date1 VARCHAR(10),#Date2 VARCHAR(10)
SET #Date1='January'
SET #Date2='DECEMBER'
;WITH CTE
AS
(
SELECT Number AS EnglishMOnthNumber,
DATENAME(MONTH,DATEADD(MONTH,NUMBER-DATEPART(MONTH,GETDATE()),GETDATE())) AS EnglishMOnth
FROM master.dbo.spt_values
WHERE NUmber Between 1 and 12
AND type='P'
)
SELECT MAX(CASE WHEN EnglishMOnth=#Date2 THEN EnglishMOnthNumber END)
- MAX( CASE WHEN EnglishMOnth=#Date1 THEN EnglishMOnthNumber END ) AS DiffInMonths
FROM CTE
I would get the difference between the required month (in your case May) and current month, If the difference is less than 0 then I would add the number of months (12) to it to get the calculation else nothing would be added. This is just one more way better answers above
declare #RequiredMonth varchar(25) = 'February';
declare #difference as int;
set #difference = MONTH(cast(#RequiredMonth+'1 2016' as datetime)) - MONTH(getdate())
select case when #difference >= 0 then #difference else #difference + 12 end
You could use something like this :
DECLARE
#month varchar(20) = 'May'
SELECT DATEDIFF(MONTH, GETDATE(), EndDate)
FROM (SELECT CAST(#month + CAST( YEAR(GETDATE()) + 1 AS VARCHAR(4)) AS DATE) EndDate) D

Sum of age between two dates in Years Month Days format

I need to calculate age in years, months and days format between two dates (DateFrom and DateTo) in a way that:
If day of month in DateFrom is 1st then take it as a whole month
If day of month in DateFrom is not 1st then count days till the end of the month.
Example:
DateFrom='2010-02-01', DateTo='2011-03-11', Age= 1 Years, 1 Months 11 Days
DateFrom='2010-02-02', DateTo='2011-03-11', Age= 1 Years, 1 Months 8 Days
After calculating the age I'd need to sum the ages assuming that month is 30 days - in the above example I'd expect result: 2 Years, 2 Months, 19 Days.
My assumption would be that you have have a table containing "DateFrom" and "DateTo" columns so the query would be something like this:
DECLARE #TotalDiffInDays int = (SELECT AVG(DATEDIFF(DAY, DateFrom, DateTo)) AS [TotalDays] FROM #t)
SELECT #TotalDiffInDays
DECLARE #DaysInMonth int = 30;
DECLARE #DaysInYear int = 365;
SELECT
#TotalDiffInDays / 365 AS AvgYearsDiff,
(#TotalDiffInDays / #DaysInMonth - #TotalDiffInDays / #DaysInYear * 12) AS AvgMonthsDiff,
#TotalDiffInDays - ((#TotalDiffInDays / 365) * #DaysInYear + (#TotalDiffInDays / #DaysInMonth - #TotalDiffInDays / #DaysInYear * 12) * #DaysInMonth) AS AvgDaysDiff
Note that in this case I am using INT division to get correct numbers.
In addition, if you just want to get dates difference in the format you desribed above you can use this query:
SELECT
DATEDIFF(YEAR, DateFrom, DateTo) AS [Years],
-- Add year diff to get corret months diff
DATEDIFF(MONTH, DATEADD(YEAR, DATEDIFF(YEAR, DateFrom, DateTo), DateFrom), DateTo) AS [Months],
-- Add months diff to get correct days diff
DATEDIFF(DAY, DATEADD(MONTH, DATEDIFF(MONTH, DateFrom, DateTo), DateFrom), DateTo) AS [Days]
FROM #t
I hope this will helps.
Yeah, just forgot about your rule of the 1st and not 1st day of the month, you can easily modify those queries with adding the DATEPART(DAY , your_date) function to check if it's a 1st date of the month and perform DETEADD() to apply this logic or just add a value of 1 before AVG.
So basically since this custom date maths, you'd need to implement your own functions to get desired results.
See the TSQL below
CREATE FUNCTION dbo.getDateSum(#d1 varchar(100), #d2 varchar(100))
RETURNS varchar(100)
WITH EXECUTE AS CALLER
AS
BEGIN
DECLARE #y int, #m int, #d int
Select
#d1= REPLACE(REPLACE(REPLACE(#d1,' years, ', '-'),' months, ','-'),' days',''),
#d2= REPLACE(REPLACE(REPLACE(#d2,' years, ', '-'),' months, ','-'),' days','')
Select
#y= CAST(LEFT(#d1,CHARINDEX('-',#d1)-1) AS INT)+CAST(LEFT(#d2,CHARINDEX('-',#d2)-1) AS INT),
#m= CAST(SUBSTRING(#d1,CHARINDEX('-',#d1)+1,LEN(#d1)-CHARINDEX('-',REVERSE(#d1))-CHARINDEX('-',#d1))AS INT)+CAST(SUBSTRING(#d2,CHARINDEX('-',#d2)+1,LEN(#d2)-CHARINDEX('-',REVERSE(#d2))-CHARINDEX('-',#d2)) AS INT),
#d= CAST(LEFT(REVERSE(#d1),CHARINDEX('-',REVERSE(#d1))-1)AS INT)+CAST(LEFT(REVERSE(#d2),CHARINDEX('-',REVERSE(#d2))-1) AS INT)
IF(#d>30)
BEGIN
SET #d=#d%30
SET #m=#m+CAST(#d/30 as INT)
END
IF(#m>30)
BEGIN
SET #m=#m%12
SET #y=#y+CAST(#m/12 as INT)
END
RETURN (cast(#y as varchar)+ ' years, ' + cast(#m as varchar) +' months, '+ cast(#d as varchar) + ' days' );
END
go
CREATE FUNCTION dbo.getDateDiff(#df date, #dt date)
RETURNS varchar(100)
WITH EXECUTE AS CALLER
AS
BEGIN
declare #y int, #m int, #d int
Select #y= YEAR(#dt)- YEAR(#df),#m= MONTH(#dt)- MONTH(#df),#d=CASE WHEN DAY(#df)=1 THEN DAY(#dt)- DAY(#df)+1 ELSE DAY(#dt)- DAY(#df) -1 END
If (#d<0)
BEGIN
Set #m=#m-1
set #d=#d + DATEDIFF(d, #dt, EOMONTH(#dt))
END
IF(#m<0)
BEGIN
Set #y=#y-1
Set #m=#m+ 12
END
RETURN (cast(#y as varchar)+ ' years, ' + cast(#m as varchar) +' months, '+ cast(#d as varchar) + ' days' )
END
go
SELECT dbo.getDateSum(dbo.getDateDiff('2010-02-01','2011-03-11'),dbo.getDateDiff('2010-02-02', '2011-03-11'))
go

Get date difference in year, month, and days SQL

Is there a way to calculate how old someone is based on today's date and their birthday then display it in following manners:
If a user is less than (<) 1 year old THEN show their age in MM & days.
Example: 10 months & 2 days old
If a user is more than 1 year old AND less than 6 years old THEN show their age in YY & MM & days.
Example: 5 years & 3 months & 10 days old
If a user is more than 6 years old THEN display their age in YY.
Example: 12 years
This is basically what you are looking for:
DECLARE #date1 DATETIME
, #date2 DATETIME;
SELECT #date1 = '1/1/2008'
, #date2 = GETDATE();
SELECT CASE
WHEN DATEDIFF(YEAR, #date1, #date2) < 1 THEN CAST(DATEDIFF(mm, #date1, #date2) AS VARCHAR)+' Months & '+CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, #date1, #date2), #date1), #date2) AS VARCHAR)+' Days'
WHEN DATEDIFF(YEAR, #date1, #date2) BETWEEN 1 AND 5 THEN CAST(DATEDIFF(mm, #date1, #date2) / 12 AS VARCHAR)+' Years & '+CAST(DATEDIFF(mm, #date1, #date2) % 12 AS VARCHAR)+' Months'
WHEN DATEDIFF(YEAR, #date1, #date2) >= 6 THEN CAST(DATEDIFF(YEAR, #date1, #date2) AS VARCHAR)+' Years'
END;
Result for when a user is less than (<) 1 year old THEN show their age in MM & days:
Result for when a user is more than 1 year old AND less than 6 years old THEN show their age in YY & MM & days:
Result for when a user is more than 6 years old THEN display their age in YY:
from this previous question
How to calculate age in T-SQL with years, months, and days
you can do procedure like this
CREATE procedure [dbo].[proc_datediff]
(
#date datetime
)
as
begin
DECLARE #diff varchar(70)
DECLARE #tmpdate datetime, #years int, #months int, #days int
SELECT #tmpdate = #date
SELECT #years = DATEDIFF(yy, #tmpdate, GETDATE()) - CASE WHEN
(MONTH(#date) > MONTH(GETDATE())) OR (MONTH(#date) =
MONTH(GETDATE()) AND DAY(#date) > DAY(GETDATE())) THEN 1 ELSE 0 END
SELECT #tmpdate = DATEADD(yy, #years, #tmpdate)
SELECT #months = DATEDIFF(m, #tmpdate, GETDATE()) - CASE WHEN
DAY(#date) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT #tmpdate = DATEADD(m, #months, #tmpdate)
SELECT #days = DATEDIFF(d, #tmpdate, GETDATE())
select #diff=
case
when #years < 1 then
concat( #months,' Months ',#days,' days ' )
when #years >=1 and #years < 6
then
concat(#years,' year ', #months,' Months ',#days,' days ' )
when #years >= 6 then
concat( #years,' years ' )
end;
select #diff
end
execute proc_datediff '1/1/2016'
go
CREATE FUNCTION [dbo].[FindDateDiff](#Date1 date,#Date2 date, #IncludeTheEnDate bit)
RETURNS TABLE
AS
RETURN
(
SELECT
CALC.Years,CALC.Months,D.Days,
Duration = RTRIM(Case When CALC.Years > 0 Then CONCAT(CALC.Years, ' year(s) ') Else '' End
+ Case When CALC.Months > 0 Then CONCAT(CALC.Months, ' month(s) ') Else '' End
+ Case When D.Days > 0 OR (CALC.Years=0 AND CALC.Months=0) Then CONCAT(D.Days, ' day(s)') Else '' End)
FROM (VALUES(IIF(#Date1<#Date2,#Date1,#Date2),DATEADD(DAY, IIF(#IncludeTheEnDate=0,0,1), IIF(#Date1<#Date2,#Date2,#Date1)))) T(StartDate, EndDate)
CROSS APPLY(Select
TempEndYear = Case When ISDATE(CONCAT(YEAR(T.EndDate), FORMAT(T.StartDate,'-MM-dd')))=1 Then CONCAT(YEAR(T.EndDate), FORMAT(T.StartDate,'-MM-dd'))
Else CONCAT(YEAR(T.EndDate),'-02-28') End
) TEY
CROSS APPLY(Select EndYear = Case When TEY.TempEndYear > T.EndDate Then DATEADD(YEAR, -1, TEY.TempEndYear) Else TEY.TempEndYear End) EY
CROSS APPLY(Select
Years = DATEDIFF(YEAR,T.StartDate,EY.EndYear),
Months = DATEDIFF(MONTH,EY.EndYear,T.EndDate)-IIF(DAY(EY.EndYear)>DAY(T.EndDate),1,0)
) CALC
CROSS APPLY(Select Days = DATEDIFF(DAY,DATEADD(MONTH,CALC.Months,DATEADD(YEAR,CALC.Years,T.StartDate)),T.EndDate)) D
)
Sample:
Select [From] = '2021-01-01',[To] = '2021-12-31',IncludeEndDate='Yes',* From dbo.FindDateDiff('2021-01-01','2021-12-31',1)
Select [From] = '2021-01-01',[To] = '2021-12-31',IncludeEndDate='No',* From dbo.FindDateDiff('2021-01-01','2021-12-31',0)
Select [From] = '2015-12-15',[To] = '2018-12-14',IncludeEndDate='Yes',* From dbo.FindDateDiff('2015-12-15','2018-12-14',1)
Select [From] = '2015-12-15',[To] = '2018-12-14',IncludeEndDate='No',* From dbo.FindDateDiff('2015-12-15','2018-12-14',0)
Probably not the most efficient way to go about it, but here's how I did it:
I had to first get the date difference between today's date and person's birthdate. I used it to get years, months, days, etc by combining it with ABS(), and Remainder (%) function.
declare #year int = 365
declare #month int = 30
declare #sixYears int = 2190
select
--CAST(DATEDIFF(mm, a.BirthDateTime, getdate()) AS VARCHAR) as GetMonth,
--CAST(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, a.BirthDateTime, getdate()), a.BirthDateTime), getdate()) AS VARCHAR) as GetDays,
CASE
WHEN
DATEDIFF(dd,a.BirthDateTime,getdate()) < #year
THEN
cast((DATEDIFF(dd,a.BirthDateTime,getdate()) / (#month)) as varchar) +' Months & ' +
CAST(ABS(DATEDIFF(dd, DATEADD(mm, DATEDIFF(mm, a.BirthDateTime, getdate()), a.BirthDateTime), getdate())) AS VARCHAR)
+ ' Days'
WHEN
DATEDIFF(dd,a.BirthDateTime,getdate()) between #year and #sixYears
THEN
cast((DATEDIFF(dd,a.BirthDateTime,getdate()) / (#year)) as varchar) +' Years & ' +
CAST((DATEDIFF(mm, a.BirthDateTime, getdate()) % (12)) AS VARCHAR) + ' Months'
WHEN DATEDIFF(dd,a.BirthDateTime,getdate()) > #sixYears
THEN cast(a.Age as varchar) + ' Years'
end as FinalAGE,
dc.DayMarker = cast(getdate() as date)
DAYmarker is your date field.

How to display the exact age in Year Month Day format in SQL Server

I am stuck with my query. I have a table called Patient. In this table a column has patient DOB. Actually I want to display the exact age of the patient.
For example:
PatName DOB (MM/dd/yyyy) Age
a 06/02/1947 65 Years 1 Month/s 3 Days
b 07/10/1947 64 Years 11 Month/s 25 Days
c ----------- -----------------------
I want to display the age of the above format.
I have already googled about this but nobody helped.
If you have any query regarding this pls post that.
Thanks
Hi Check the query below.
--DROP TABLE patient
CREATE TABLE patient(PatName varchar(100),DOB date, Age varchar(100))
INSERT INTO patient
VALUES('a','06/02/1947',NULL),('b','07/10/1947',NULL),('c','12/21/1982',NULL)
;WITH CTE(PatName,DOB,years,months,days) AS
(SELECT PatName,DOB,DATEDIFF(yy,DOB,getdate()),DATEDIFF(mm,DOB,getdate()),DATEDIFF(dd,DOB,getdate()) FROM patient)
--SELECT * FROM CTE
SELECT PatName,DOB,
CAST(months/12 as varchar(5))+' Years'+CAST((months % 12) as varchar(5))+' month/s '+CAST(CASE WHEN DATEADD(MM,(months % 12),DATEADD(YY,(months/12),DOB)) <= GETDATE() then DATEDIFF(dd,DATEADD(MM,(months % 12),DATEADD(YY,(months/12),DOB)),GETDATE()) ELSE DAY(getdate()) END as varchar(5))+' days' as Age
FROM CTE
If you follow this link, you'll find a function doing exactly that:
create function dbo.F_AGE_YYYY_MM_DD
(
#START_DATE datetime,
#END_DATE datetime
)
returns varchar(10)
as
/*
Function: F_AGE_YYYY_MM_DD
This function calculates age in years, months and days
from #START_DATE through #END_DATE and
returns the age in format YYYY MM DD.
Years is the number of full years between #START_DATE and #END_DATE.
Months is the number of full months since the last full year anniversary.
Days is the number of days since the last full month anniversary.
*/
begin
declare #AGE varchar(10)
declare #AGE_IN_YEARS int
declare #AGE_IN_MONTHS int
declare #AGE_IN_DAYS int
-- Return null if #START_DATE > #END_DATE
if #START_DATE > #END_DATE begin return #AGE end
select
#AGE_IN_YEARS = AGE_IN_YEARS,
#AGE_IN_MONTHS = AGE_IN_MONTHS,
#AGE_IN_DAYS =
datediff(dd,
dateadd(mm,AGE_IN_MONTHS,
dateadd(yy,AGE_IN_YEARS,START_DATE))
,END_DATE)
from
(
select
AGE_IN_MONTHS =
case
when AnniversaryThisMonth <= END_DATE
then datediff(mm,dateadd(yy,AGE_IN_YEARS,START_DATE),END_DATE)
else datediff(mm,dateadd(yy,AGE_IN_YEARS,START_DATE),END_DATE)-1
end,
*
from
(
select
AGE_IN_YEARS =
case
when AnniversaryThisYear <= END_DATE
then datediff(yy,START_DATE,END_DATE)
else datediff(yy,START_DATE,END_DATE)-1
end,
*
from
(
select
AnniversaryThisYear =
dateadd(yy,datediff(yy,START_DATE,END_DATE),START_DATE),
AnniversaryThisMonth =
dateadd(mm,datediff(mm,START_DATE,END_DATE),START_DATE),
*
from
(
select START_DATE = dateadd(dd,datediff(dd,0,#START_DATE),0),
END_DATE = dateadd(dd,datediff(dd,0,#END_DATE),0)
) aaaa
) aaa
) aa
) a
select #AGE =
right('0000'+convert(varchar(4),#AGE_IN_YEARS),4) + ' ' +
right('00'+convert(varchar(4),#AGE_IN_MONTHS),2) + ' ' +
right('00'+convert(varchar(4),#AGE_IN_DAYS),2)
return #AGE
end
go
select [Age] = dbo.F_AGE_YYYY_MM_DD('2004-04-07','2006-02-03')
select [Age] = dbo.F_AGE_YYYY_MM_DD('2006-02-03','2006-02-03')
select [Age] = dbo.F_AGE_YYYY_MM_DD('2006-02-05','2006-02-03')
select [Age] = dbo.F_AGE_YYYY_MM_DD('1950-09-13', getdate())
DECLARE #date datetime, #tmpdate datetime, #years int, #months int, #days int
SELECT #date = '06/02/1947'
SELECT #tmpdate = #date
SELECT #years = DATEDIFF(yy, #tmpdate, GETDATE()) - CASE WHEN (MONTH(#date) > MONTH(GETDATE())) OR (MONTH(#date) = MONTH(GETDATE()) AND DAY(#date) > DAY(GETDATE())) THEN 1 ELSE 0 END
SELECT #tmpdate = DATEADD(yy, #years, #tmpdate)
SELECT #months = DATEDIFF(m, #tmpdate, GETDATE()) - CASE WHEN DAY(#date) > DAY(GETDATE()) THEN 1 ELSE 0 END
SELECT #tmpdate = DATEADD(m, #months, #tmpdate)
SELECT #days = DATEDIFF(d, #tmpdate, GETDATE())
print cast(#years as varchar(4)) + ' years, '+ cast (#months as varchar(2))+' months, '+ cast(#days as varchar(2)) + ' days'
Here is the solution
DATEDIFF(year, DOB, getdate()) - (CASE WHEN (DATEADD(year, DATEDIFF(year, DOB, getdate()), DOB)) > getdate() THEN 1 ELSE 0 END) as Years,
MONTH(getdate() - (DATEADD(year, DATEDIFF(year, DOB, getdate()), DOB))) - 1 as Month/s,
DAY(getdate() - (DATEADD(year, DATEDIFF(year, DOB, getdate()), DOB))) - 1 as Days