SQL Server 获取七月的年龄[duplicate]

qhhrdooz  于 2022-11-21  发布在  其他
关注(0)|答案(2)|浏览(103)

此问题在此处已有答案

How to calculate age (in years) based on Date of Birth and getDate()(39个答案)
11天前关闭。
我有一个以DateTime格式存储患者出生日期的表(2022-06-22)。
我怎样才能得到七月出生的人的年龄?
我试过下面的:

select sFirstName,
sLastName,
dDateOfBirth,
DATEDIFF(yy,dDateOfBirth,GETDATE()) as Age,
ifkMedicalAidID
from Patients
where DATEDIFF(month,dDateOfBirth,GETDATE()) between 5 and 5

虽然我只得到了七月的所有记录(这是正确的),所有年龄都是0,但我看到了上述查询的问题,但我没有SQL知识来解决它...

o2rvlv0m

o2rvlv0m1#

SELECT 
FLOOR((DATEDIFF(DAY,0,GETDATE()) - DATEDIFF(DAY,0,dDateOfBirth)) / 365.2425) AS AgeYears
mmvthczy

mmvthczy2#

这个管用,谢谢约翰

select sFirstName,
    sLastName,
    dDateOfBirth,
    DATEDIFF(yy,dDateOfBirth,GETDATE()) as Age,
    ifkMedicalAidID
    from Patients
    where   datepart(month,dDateOfBirth)=7

相关问题