Попробуйте это ....
SELECT CLAIM.BTHDAT, DATEDIFF(hour,CLAIM.BTHDAT,GETDATE())/8766.0 AS AgeYearsDecimal
,CONVERT(int,ROUND(DATEDIFF(hour,CLAIM.BTHDAT,GETDATE())/8766.0,0)) AS AgeYearsIntRound
,DATEDIFF(hour,CLAIM.BTHDAT,GETDATE())/8766 AS AgeYearsIntTrunc FROM CLAIM
Если вы хотите, чтобы Возраст в день, месяц и год, то вы можете попробовать это. Создать функцию для расчета возраста и вызвать его в запросе, как это
Create function [dbo].[GetAge](@dayOfBirth datetime, @today datetime)
RETURNS varchar(100)
AS
Begin
DECLARE @tmpdate datetime, @date datetime, @years int, @months int, @days int
SELECT @tmpdate = @dayOfBirth
SELECT @years = DATEDIFF(yy, @tmpdate, @today) - CASE WHEN (MONTH(@tmpdate) > MONTH(@today)) OR (MONTH(@tmpdate) = MONTH(@today) AND DAY(@tmpdate) > DAY(@today)) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(yy, @years, @tmpdate)
SELECT @months = DATEDIFF(m, @tmpdate, @today) - CASE WHEN DAY(@tmpdate) > DAY(@today) THEN 1 ELSE 0 END
SELECT @tmpdate = DATEADD(m, @months, @tmpdate)
SELECT @days = DATEDIFF(d, @tmpdate, @today)
return cast(@years as varchar(2)) + ' years,' + cast(@months as varchar(2)) + ' months,' + cast(@days as varchar(3)) + ' days'
end
Select CLAIM.BTHDAT,dbo.[GetAge](CLAIM.BTHDAT,getdate()) From CLAIM