呵呵
--当天
select * 
from 表
where (month(getdate()) = month(birthDay) and day(getdate()) = day(birthDay))
--本月、下月
select * 
from 表
where month(birthDay) - month(getdate()) in (0,1,-11)

解决方案 »

  1.   

    ----创建测试数据
    declare @Employee table(id int,birthday datetime)
    insert @Employee
    select 1,'2006-12-26' union all
    select 2,'2006-12-27' union all
    select 3,'2007-01-01' union all
    select 4,'2007-01-11' union all
    select 5,'2007-02-01'----查询
    select *,'今天过生日' from @Employee where datediff(dd,birthday,getdate()) = 0
    union all
    select *,'本月过生日' from @Employee where datediff(month,birthday,getdate()) = 0
    union all
    select *,'下月过生日' from @Employee where datediff(month,birthday,getdate()) = -1/*结果
    id      birthday             
    ------------------------------------------------
    2       2006-12-27 00:00:00.000    今天过生日
    1       2006-12-26 00:00:00.000    本月过生日
    2       2006-12-27 00:00:00.000    本月过生日
    3       2007-01-01 00:00:00.000    下月过生日
    4       2007-01-11 00:00:00.000    下月过生日
    */
      

  2.   

    原因是getdate()函数产生的时间是2006年.如果是以具体的日期作为参照,需要将getdate()函数替换成参照日期.
      

  3.   

    呵呵,谢谢mengmou()mengmou() 这位大哥了。
    不过hellowork(一两清风) 这位大哥写的我怎么看不明白了啊。
    能不能说清楚点啊,小弟我初学。不好意思啊
      

  4.   

    现有Employees表,字段birthDay。现在要查找当天,本月和下月过生日的员工。当天
    select * from Employees where right(convert(varchar(10),birthday,120),5) = right(convert(varchar(10),getdate(),120),5)
    本月
    select * from Employees where month(birthday) = month(getdate())
    下月
    select * from Employees where (month(birthday) = month(getdate())+1 and month(getdate()) < 12) or (month(birthday) = 1 and month(getdate()) = 12)
      

  5.   

    mengmou()mengmou() 那个参数:-11是做什么用的
      

  6.   

    多谢二楼mengmou()mengmou() 朋友的指正,改正如下:
    ----创建测试数据
    declare @Employee table(id int,birthday datetime)
    insert @Employee
    select 1,'1996-12-26' union all
    select 2,'1997-12-27' union all
    select 3,'1995-01-01' union all
    select 4,'1995-01-11' union all
    select 5,'1996-02-01'----查询
    select *,'今天过生日' from @Employee where month(birthday) = month(getdate()) and day(birthday) = day(getdate())
    union all
    select *,'本月过生日' from @Employee where month(birthday) = month(getdate())
    union all
    select *,'下月过生日' from @Employee where month(birthday) - month(getdate()) in(1,-11)/*结果
    id      birthday
    2       1997-12-27 00:00:00.000 今天过生日
    1       1996-12-26 00:00:00.000 本月过生日
    2       1997-12-27 00:00:00.000 本月过生日
    3       1995-01-01 00:00:00.000 下月过生日
    4       1995-01-11 00:00:00.000 下月过生日
    */
      

  7.   

    select *,'下月过生日' 
    from @Employee 
    where month(birthday) = month(dateadd(month,1,getdate()))