1.bzscs(沙虫 我爱小美)用函數的好辦法:
CREATE function [dbo].[calc_date](@time smalldatetime,@now smalldatetime)
returns nvarchar(10)
as
begin
declare @year int,@month int,@day int
select @year = datediff(yy,@time,@now)
if (month(@now)=month(@time)) and (day(@now)<day(@time)) or (month(@now)<month(@time))
set @year = @year-1
select @month = datediff(month,@time,@now)-12*@year
if(day(@now)<day(@time))
set @month = @month-1
select @day = datediff(dd,dateadd(month,(12*@year+@month),@time),@now)
return cast(@year as varchar) + '年'+ cast(@month as varchar)+'個月'+cast(@day as varchar)+'天'
end
GO
-------------------------------------------------------------------------------------
--調用函數 :
--1.某一具體日期
declare @kk nvarchar(10)
select @kk = [dbo].[calc_date]('2001-11-28',getdate())
print @kk
--2.數據表的'入職日期'字段
select empno,indate,[dbo].[calc_date](indate,getdate()) from employee