我确实有一个要求,比如我需要用sysdate来计算一个人的年龄。使用sysdate计算人员年龄应该大于或小于75岁和09个月。但在一些scnearios它没有成功。可以帮助解决这个问题。
IF TO_CHAR(bday,'mm/dd')='02/29' THEN
age = ADD_MONTHS(bday-1,12*75);
ELSE
age = ADD_MONTHS(bday,12*75);
END IF;
但这在某些情况下无效。任何人都可以帮助满足这一要求
答案 0 :(得分:1)
试试这个:
查找年份:查找当前日期与DOB之间的总天数,并将其除以365。
查找月份:查找剩余天数并将其除以30。
DECLARE @DOB DATETIME
DECLARE @Days INT
DECLARE @Year INT
DECLARE @Month INT
DECLARE @DaysRemaining INT
SET @DOB ='9/4/1938'
SET @Days = DATEDIFF(DAY,@DOB,GETDATE())
SET @Year = @Days / 365
SET @DaysRemaining = @Days - (365 * @Year)
SET @Month = @DaysRemaining / 30
SELECT @DOB AS DOB, CAST(@Year AS VARCHAR) + ' years ' + CAST(@Month AS VARCHAR) + ' months' AS Age
尝试使用此逻辑并应用您的条件。
答案 1 :(得分:0)
答案 2 :(得分:0)
DECLARE @DOB DATETIME
DECLARE @RequiredAgeInMonths INT
DECLARE @ActualAgeInMonths INT
SET @DOB ='9/4/1938'
SET @RequiredAgeInMonths=909
SET @ActualAgeInMonths = (SELECT DATEDIFF(MM,@DOB,GETDATE()))
IF(@ActualAgeInMonths = @RequiredAgeInMonths)
PRINT 'YOUR AGE IS 75 YEARS AND 9 MONTHS'
ELSE
PRINT 'YOUR AGE IS NOT EQUAL TO 75 YEARS AND 9 MONTHS'