本文介绍了什么是sql查询来获取yrs和月份的日期差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
什么是sql查询来获取yrs和months的日期差异..如果没有一年它不应该显示,所以几个月......
what is sql query to get date diff in yrs and months..If no year it shouldn''t be show and so for months...
推荐答案
SELECT
(CASE
WHEN ( cast(DATEDIFF(Year, '2012-04-26', '2013-03-15') as int) = 0) THEN cast(DATEDIFF(MONTH, '2012-04-26', '2013-03-15') as varchar) +' Month'
ELSE cast(DATEDIFF(Year, '2011-04-26', '2013-03-15')as varchar) +'Years '+ cast(DATEDIFF(MONTH, '2012-04-26', '2013-03-15')as varchar) +'Month' end) as diff;
select
DATEDIFF(year, '1/1/1960', GETDATE()) AS Totalyears,
DATEDIFF(month, '1/1/1960', GETDATE()) AS TotalMonths,
DATEDIFF(month, '1/1/1960', GETDATE())/12 AS Years02,
DATEDIFF(month, '1/1/1960', GETDATE())%12 AS RemainingMonths
这篇关于什么是sql查询来获取yrs和月份的日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!