楼主好,代码如下,自己写的,应该是能够满足你的需求。
CREATE
PROCEDURE [dbo].[GetTime]
@BirthDay nvarchar(20)
as
begin
declare @result nvarchar(20)
if DATEDIFF(YEAR,@BirthDay,GETDATE())>=14
begin
set @result= cast(DATEDIFF(YEAR,@BirthDay,GETDATE()) as nvarchar(6))+'岁'
end
if DATEDIFF(YEAR,@BirthDay,GETDATE())<14 and DATEDIFF(MONTH,@BirthDay,GETDATE())>=12
begin
if MONTH(GETDATE())-MONTH(@BirthDay)<0
set @result=cast(DATEDIFF(YEAR,@BirthDay,GETDATE())-1 as nvarchar(4))+'年零'+cast(datediff(mm,@BirthDay,GETDATE())%12 as nvarchar(4))+'月'
else
set @result=cast(DATEDIFF(YEAR,@BirthDay,GETDATE()) as nvarchar(4))+'年零'+cast(datediff(mm,@BirthDay,GETDATE())%12 as nvarchar(4))+'月'
end
if DATEDIFF(MONTH,@BirthDay,GETDATE())<12
begin
--当今天的日期号减去出生日期的日期号小于0,则判定为相对的月份天数不足一个月,则需要在计算月的时候减去1
--例如出生日期为2月20号,今天是4月15号,则时间差应该为1个月零26天,而不是两个月
if DAY(GETDATE())-DAY(@BirthDay)<0
set @result=cast(case when DATEDIFF(MONTH,@birthday,GETDATE())-1<0 then 0 else DATEDIFF(MONTH,@birthday,GETDATE())-1 end as nvarchar(4))+'月零'+cast(DATEDIFF(DAY,@birthday,GETDATE())-DATEDIFF(DAY,left(@birthday,7)+'-01',left(CONVERT(NVARCHAR(20),GETDATE(),23),7)+'-01') as nvarchar(4))+'天'
else
set @result=cast(DATEDIFF(MONTH,@birthday,GETDATE()) as nvarchar(4))+'月零'+cast(DATEDIFF(DAY,@birthday,GETDATE())-DATEDIFF(DAY,left(@birthday,7)+'-01',left(CONVERT(NVARCHAR(20),GETDATE(),23),7)+'-01') as nvarchar(4))+'天'
end
select @result
END
GO