I need to subtract 2 days from each other and display it in YYMMDD.
E.g.
2016-05-27 (Minus) 2015-01-15 = 1 Years, 5 Months, 16 Days
The two dates are in DATETIME
format.
Many thanks!
I need to subtract 2 days from each other and display it in YYMMDD.
E.g.
2016-05-27 (Minus) 2015-01-15 = 1 Years, 5 Months, 16 Days
The two dates are in DATETIME
format.
Many thanks!
I think this works.
SELECT Cast(DATEDIFF(YEAR, '2015-01-15', '2016-05-27') as varchar(25)) + ' Years, '
+ Cast(DATEDIFF(MONTH, '2015-01-15', '2016-05-27') % 12 as varchar(25)) + ' Months, '
+ Cast(DATEDIFF(DAY, Dateadd(Month, DATEDIFF(MONTH, '2015-01-15', '2016-05-27') % 12 ,Dateadd(Year, DATEDIFF(YEAR, '2015-01-15', '2016-05-27'), '2015-01-15')), '2016-05-27') as varchar(25)) + ' Days'
Result:
1 Years, 4 Months, 12 Days
You might take a look at this documentation https://msdn.microsoft.com/en-us/library/ms189794.aspx
DATEDIFF ( datepart , startdate , enddate )
Then get the date with the number of Days
Then you can use DATEADD
to add this number of Days.
SELECT DATEADD(dd, 0, DATEDIFF(dd, startdate, enddate))
Try this query...
select (datediff(dd,convert(datetime,'5/5/2014',101),getdate())/365) years,
((datediff(dd,convert(datetime,'5/5/2014',101),getdate()) % 365)/30) months,
((datediff(dd,convert(datetime,'5/5/2014',101),getdate()) % 365)%30) days
Result
years months days
1 10 1