什么是sql查询来获取yrs和月份的日期差异 [英] what is the sql query to get date diff in yrs and months

查看:120
本文介绍了什么是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...

推荐答案

尝试此查询

try this query
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


检查: http://stackoverflow.com/questions/57599/how-to-calculate-age-in-t-sql-with-years-months-and-天 [ ^ ]


这篇关于什么是sql查询来获取yrs和月份的日期差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆