CREATE FUNCTION [fun_lx_age](@birthday VARCHAR(10),
@nowday VARCHAR(10))
RETURNS VARCHAR(20)
AS
BEGIN
DECLARE @year INT,
@month INT,
@day INT
DECLARE @RETURN VARCHAR(20)
set @birthday=convert(varchar(10),convert(datetime,@birthday),21)
SET @day=DATEDIFF(DAY,CONVERT(DATETIME,@birthday),@nowday)-1
IF @day<0
BEGIN
SET @RETURN=''
END
ELSE
BEGIN
SET @year=FLOOR((@day-(CONVERT(NUMERIC(14,3),CONVERT(VARCHAR(4),@nowday,21))-convert(NUMERIC(14,3),CONVERT(VARCHAR(4),@birthday)))/4)/365)
SET @month=FLOOR((@day-@year*365)/30)
SET @day=@day-@year*365-@month*30
IF @year<=0
BEGIN
IF @month<=0
BEGIN
SET @RETURN=CONVERT(VARCHAR(2),@day)+'天'
END
ELSE
BEGIN
SET @RETURN=CONVERT(VARCHAR(2),@month)+'月'+ CONVERT(VARCHAR(2),@day)+'天'
end
END
ELSE
BEGIN
IF @year<=5
BEGIN
IF @month<=0
BEGIN
SET @RETURN=CONVERT(VARCHAR(2),@year)+'岁'+CONVERT(VARCHAR(2),@day)+'天'
END
ELSE
BEGIN
SET @RETURN=CONVERT(VARCHAR(2),@year)+'岁'+CONVERT(VARCHAR(2),@month)+'月'
END
END
ELSE
BEGIN
SET @RETURN=CONVERT(VARCHAR(2),@year)+'岁'
END
END
END
RETURN @RETURN
END
发表评论 / 取消回复