您的位置:首页 > 其它

计算某个日期到今天一共有多少年多少月多少日

2009-04-06 01:32 701 查看
比如是07年9月1号,到现在有多少年零几个月零几天这样,不是要他们的总数。是类似按有多少天,再转换成有几年零几个月零几天,就是算工龄一样。



declare @date datetime
set @date = '20070901'

declare @year int, @month int, @day int

set @year = datediff(year, @date, getdate()) - (case when month(@date)>month(getdate()) then 1 when month(@date)=month(getdate()) and day(@date)>day(getdate()) then 1 else 0 end)
set @month = datediff(month, @date, getdate()) - (case when day(@date)>day(getdate()) then 1 else 0 end) - 12*@year
set @day = datediff(day, dateadd(month, (@year*12 + @month), @date), getdate())

select 年 = @year, 月 = @month, 天 = @day





年 月 天
----------- ----------- -----------
X X X

(1 行受影响)



http://topic.csdn.net/u/20090406/00/21d8efda-0644-434b-bb07-9140c3cd26b3.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐