樓主好,代碼如下,自己寫的,應該是能夠滿足你的需求。
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