0

我有以下代码可以从今天的日期计算一个人的出生日期,效果很好

    SELECT application.applicationLastName, application.applicationFirstName, application.applicationDoB, 
ROUND(DATEDIFF(NOW(), applicationDoB) / 365.2425,1) AS age, application.applicationID, application.applicationAcknowledged, application.applicationContacted, application.applicationDate
FROM application
ORDER BY application.applicationDate ASC 

但是,我被要求从本月 1 日开始计算他们的年龄并尝试过

SELECT application.applicationLastName, application.applicationFirstName, application.applicationDoB, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS CURMONTH, 
ROUND(DATEDIFF(CURMONTH(), applicationDoB) / 365.2425,1) AS age, application.applicationID, application.applicationAcknowledged, application.applicationContacted, application.applicationDate
FROM application
ORDER BY application.applicationDate ASC

但是我收到以下错误:调用本机函数'DATEDIFF'中的参数计数不正确

4

2 回答 2

0

你不应该使用CURMONTH(). 而是将今天的日期格式化为本月 1 日。

使用以下查询:

SELECT application.applicationLastName, application.applicationFirstName, application.applicationDoB, DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), 0) AS CURMONTH, 
ROUND(DATEDIFF(DATE_FORMAT(NOW() ,'%Y-%m-01'), applicationDoB) / 365.2425,1) AS age, application.applicationID, application.applicationAcknowledged, application.applicationContacted, application.applicationDate
FROM application
ORDER BY application.applicationDate ASC
于 2015-12-11T16:24:22.333 回答
0

您正在使用: DATEDIFF(CURMONTH(), applicationDoB)

在你的代码中。但是 CURMONTH() 只给你当前日期的月份,而 applicationDoB 给你一个完整的日期。您还需要将 CURMONTH() 替换为对应于完整日期的表达式(当月的第一天)。

于 2015-12-11T16:29:29.937 回答