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 人员信息表

posted @ 2018-06-13 00:30  小美lmt  阅读(1135)  评论(0编辑  收藏  举报