Código:
create function edad(@nacimiento as datetime) returns varchar(25) as begin declare @aYear varchar(3) declare @diasTranscurridos varchar(3) set @aYear = year(getdate()) - year(@nacimiento) if month(getdate()) < month(@nacimiento) begin set @aYear = @aYear - 1 set @diasTranscurridos = datediff(day,convert(varchar,year(getdate())) + '/01/01',getdate())+1 set @diasTranscurridos = 365 - @diasTranscurridos end else begin if month(getdate()) > month(@nacimiento) begin set @diasTranscurridos = datediff(day,convert(varchar,year(getdate())) + '/01/01',getdate())+1 end else begin if month(getdate()) = month(@nacimiento) begin if day(getdate()) = day(@nacimiento) begin set @aDia = 0 end else begin if day(getdate()) < day(@nacimiento) begin set @aYear = @aYear - 1 set @diasTranscurridos = datediff(day,convert(varchar,year(getdate()) - 1) + '/01/01',convert(varchar,year(getdate()) - 1) + '/31/12') - (datediff(day,convert(varchar,year(getdate())) + '/' + convert(varchar,month(getdate())) + '/01',getdate()) + 1) end else begin if day(getdate()) > day(@nacimiento) begin set @diasTranscurridos = datediff(day,convert(varchar,year(getdate())) + '/' + convert(varchar,month(getdate())) + '/01',getdate())+1 end end end end end end return @aYear + ' años ' + @diasTranscurridos + ' días' end