【发布时间】:2014-10-18 15:55:32
【问题描述】:
SELECT * FROM hr.Employees;
SELECT firstname,
lastname,
(CASE WHEN (month(getdate()) > month(birthdate))
THEN datediff(yy,birthdate,getdate())
ELSE datediff(yy,birthdate,getdate()) -1
END) AS _years, --years
(CASE WHEN (month(getdate()) > month(birthdate))
THEN month(getdate()) - month(birthdate)
ELSE month(getdate()) - month(birthdate) +12
END) AS _months, --months
(CASE WHEN (day(getdate()) > (day(birthdate)))
THEN day(getdate()) - day(birthdate)
ELSE month(getdate()) - month(birthdate) +11 & day(getdate()) + (day(EOMonth(birthdate)) - day(birthdate))
END) AS _days --days
FROM hr.Employees
这里是在 yy,mm,dd 中查找年龄的代码。我遇到的问题是在天部分。
我想做的是(if current day < birthdate day ) 然后(currentMonth-birthmonht)+11 and day(getdate()) + (day(EOMonth(birthdate)) - day(birthdate))。
【问题讨论】:
-
这是非常糟糕的格式,SQL 看起来很糟糕。
标签: sql sql-server sql-server-2012