我需要相互减去2天并在YYMMDD中显示。
E.g。
2016-05-27 (Minus) 2015-01-15 = 1 Years, 5 Months, 16 Days
这两个日期采用DATETIME
格式。
非常感谢!
答案 0 :(得分:1)
我认为这很有效。
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'
结果:
1 Years, 4 Months, 12 Days
答案 1 :(得分:0)
您可以查看此文档 https://msdn.microsoft.com/en-us/library/ms189794.aspx
DATEDIFF(datepart,startdate,enddate)
然后获取日期天数
然后您可以使用DATEADD
添加此天数。
SELECT DATEADD(dd, 0, DATEDIFF(dd, startdate, enddate))
答案 2 :(得分:0)
尝试此查询...
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
结果
years months days
1 10 1