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

    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


但是,我被要求从当前月的第一天开始计算他们的年龄,并尝试

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”时参数计数不正确

最佳答案

您不应该使用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

07-24 15:36