SQL查询【根据生日计算】

时间:2023-03-09 19:30:16
SQL查询【根据生日计算】

根据生日日期,获取当前年龄、年龄单位。

Select Case when DateDiff(Year, BirthDate, GetDate()) > 0 then DateDiff(Year, BirthDate, GetDate())
                    when DateDiff(Month, BirthDate, GetDate()) > 0 then DateDiff(MONTH, BirthDate, GetDate())
                    When DATEDIFF(DAY, BirthDate, GetDate()) > 0 then DateDiff(DAY, BirthDate, GetDate()) end Age,
           Case When DateDiff(Year, BirthDate, GetDate()) > 0 then '岁'
                    When DateDiff(Month, BirthDate, GetDate()) > 0 then '月'
                    When DATEDIFF(DAY, BirthDate, GetDate()) > 0 then '日' end AgeUnit, BirthDate BirthDate
From 人员信息表