繁体   English   中英

SQL根据年和月计算年龄

[英]SQL to calculate age based on year and month

我想获得一个名单,这些名单的年龄分别为14岁和11个月。 有人可以帮我修复下面的SQL吗? 谢谢!

SELECT NAME
FROM TABLE
WHERE
(cast(datediff(DAY, DOB, getDate() -1) /(365.23076923074) as int)>=14 
    AND cast(datediff(MONTH, DOB, getDate() -1) % (12) as int)>=11)
OR      
(cast(datediff(DAY, DOB, getDate() -1) /(365.23076923074) as int)<=15 
    AND cast(datediff(MONTH, DOB, getDate() -1) % (12) as int)<=0)

这些人今天已经14、11个月了。

select name 
from table
where DOB = cast(dateadd(mm,-11,dateadd(yy,-14,getdate())) as date)
--this is to eliminate the time on getdate

我不确定您是否需要<=或仅< 请相应地进行更改。

select NAME from table    Where DOB < = DATEADD(MONTH, -179, GETDATE())

或者,如果您只是在寻找一个月范围内的结果,则:

select NAME from table Where DOB BETWEEN DATEADD(MONTH, -179, GETDATE()) AND DATEADD(MONTH, -180, GETDATE())

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM