How Calculate Expire Date in SQL Query - sql

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.

Related

Calculating age in a certain month in SQL Server

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)

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.

SQL Get "ISO Year" for ISO Week

I need to calculate the year a week is assigned to. For example the 29th december of 2003 was assigned to week one of year 2004 (this is only for europe, I think). You can take a look at this with this code:
SELECT DATEPART(isowk, '20141229');
But now I need an easy way to get the year this week is assigned to. What I currently do is not that elegant:
DECLARE #week int, #year int, #date char(8)
--set #date = '20150101'
set #date = '20141229'
SET #week = cast(datepart(isowk, #date) as int)
if #week = 1
begin
if DATEPART(MONTH, #date) = 12
begin
set #year = DATEPART(year, #date) + 1
end
else
begin
set #year = DATEPART(year, #date)
end
end
select #date "DATE", #week "WEEK", #year "YEAR"
If anybody knew a more elegant way, that would be nice :-)
This solution The code in the question does not return the correct value for the date '1-1-2027'.
The following will return the correct value with all dates i tested (and i tested quite a few).
SELECT YEAR(DATEADD(day, 26 - DATEPART(isoww, '2012-01-01'), '2012-01-01'))
As taken from: https://capens.net/content/sql-year-iso-week
This is the most compact solution I could come up with:
CASE
WHEN DATEPART(ISO_WEEK, #Date) > 50 AND MONTH(#Date) = 1 THEN YEAR(#Date) - 1
WHEN DATEPART(ISO_WEEK, #Date) = 1 AND MONTH(#Date) = 12 THEN YEAR(#Date) + 1
ELSE YEAR(#Date) END
Can be used directly inside a SELECT statement. Or you could consider creating a user-defined function that takes the #Date parameter as input and outputs the result of the case statement.
I think this solution is much more logical and easier to comprehend for ISO-8601.
"The first week of the year is the week containing the first Thursday."
see ISO Week definition
So we need to deduct the weekday of the given date from Thursday and add this to that same date to get the year.
Adding 5 and then taking the modulus of 7 to move Sunday to the previous week.
declare #TestDate date = '20270101'
select year(dateadd(day, 3 - (datepart(weekday, #TestDate) + 5) % 7, #TestDate))
This will result in 2026 which is correct.
This will give the correct result for all dates I check between 1990-2100 using this:
declare #TestDate date = '19900101'
declare #Results as table
(
TestDate date,
FirstDayofYear varchar(20),
ISOYear int,
ISOWeek int
)
while (#TestDate < '21000201')
begin
insert #Results(TestDate, FirstDayofYear, ISOYEar, ISOWeek)
select #TestDate, datename(weekday, dateadd(day, datepart(day, #TestDate) * -1 +1, #TestDate)),
year(dateadd(day, 3 - (datepart(weekday, #TestDate) + 5) % 7, #TestDate)), datepart(ISOWK, #TestDate)
set #TestDate = dateadd(day, 1, #Testdate)
if(datepart(day, #TestDate) > 7)
begin
set #TestDate = dateadd(year, 1, dateadd(day, datepart(day, #TestDate) * -1 + 1, #TestDate))
end
end
-- Show all results that are wrong:
select * from #Results
where (ISOYear <> datepart(year, TestDate) and ISOWeek < 3)
or (ISOYear = datepart(year, TestDate) and ISOWeek >= 52)
I think Bart Vanseer solution is nice and simple, but off by 1.
I believe it should be
select year(dateadd(day, 3 - ((datepart(weekday, #TestDate) + 5) % 7) , #TestDate))
Try following to find a few places were it differs
declare #TestDate date = '2000-01-01'
DECLARE #cnt INT = 0;
DECLARE #cnt_total INT = 10000;
WHILE #cnt < #cnt_total
BEGIN
SET #TestDate = dateadd(day,1, #TestDate)
if year(dateadd(day, 3 - (datepart(weekday, #TestDate) + 6) % 8, #TestDate)) <>
year(dateadd(day, 3 - ((datepart(weekday, #TestDate) + 5) % 7) , #TestDate))
BEGIN
select #TestDate, year(dateadd(day, 3 - (datepart(weekday, #TestDate) + 6) % 8, #TestDate))
select #TestDate, year(dateadd(day, 3 - ((datepart(weekday, #TestDate) + 5) % 7) , #TestDate))
END
SET #cnt = #cnt + 1;
END;
Inspired by the other answers, i came to the following solution:
declare #TestDate date = '20270101'
SELECT DATEPART(year, DATEADD(day, 4 - DATEPART(weekday, #TestDate), #TestDate))
Just get thursday of the current week to get the correct year.
Assuming DATEFIRST is set to Monday (1).
DECLARE #date DATETIME
SET #date='2014-12-29'
SELECT
CASE --Covers logic for ISO week date system which is part of the ISO 8601 date and time standard. Ref: https://en.wikipedia.org/wiki/ISO_week_date
WHEN (DATEPART(ISO_WEEK,#date) = 53) AND (DATEPART(MONTH,#date) = 1)
THEN CAST((DATEPART(YEAR, #date) - 1) AS varchar(4)) + ('-W') + CAST (RIGHT('0' + CAST(DATEPART(ISO_WEEK,#date) AS varchar(2)),2) AS varchar(2))
WHEN (DATEPART(ISO_WEEK,#date) = 1) AND (DATEPART(MONTH,#date) = 12)
THEN CAST((DATEPART(YEAR,#date) + 1) AS varchar(4)) + ('-W') + CAST (RIGHT('0' + CAST(DATEPART(ISO_WEEK,#date) AS varchar(2)),2) AS varchar(2))
ELSE CAST(DATEPART(YEAR,#date) AS varchar(4)) + ('-W') + CAST (RIGHT('0' + CAST(DATEPART(ISO_WEEK,#date) AS varchar(2)),2) AS varchar(2))
END AS ISO_week
For IsoYear, get the Thursday from IsoWeek, and then get the year.

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