[CODE=SQL]
考虑了闰年问题。
select
name,birthdate,
case when BDCur>=today then BDCur else BDNxt end as Birthday
from
(
select name,birthdate,today,
BDCur + case when day(birthdate) =29 and day(BDCur)=28
then 1 else 0 end as BDCur,
BDNxt + case when day(birthdate)=29 and day(BDNxt)=28
then 1 else 0 end as BDNxt
from
(
select name,birthdate,today,
dateadd(year,diff,birthdate)as BDCur,
dateadd(year,diff+1,birthdate) as BDNxt
from
(
select name,birthdate,
datediff(year,birthdate,getdate()) as diff,
cast(convert(char(8),getdate(),112) as datetime) as today from your_data_table) as t1
)as t2
)as t3
假设存在某闰年2月29日生日的people,如果今年生日已过,而明年为闰年,那么查询结果birthday是?年2月29日,否则为?年3月1日。
[code=SQL]
TRY
select * from table where CAST( datediff(day,'2000-'+right(CONVERT(VARCHAR(10),生日,23),5),'2000-'+right(CONVERT(VARCHAR(10),GETDATE(),23),5)) AS INT)<3
TRY
select * from table where CAST( datediff(day,CONVERT(VARCHAR(10),'2000'+right(CONVERT(VARCHAR(10),生日,23),6),23),CONVERT(VARCHAR(10),'2000'+right(CONVERT(VARCHAR(10),GETDATE(),23),6) ,23)) AS INT)<3