查询出下月过生日的员工。
要考虑到闰年什么的。。说是要用到CASE WHEN。。
头大了。

解决方案 »

  1.   

    case when year(birthday)%4=0 then '闰年' else '非闰年' end
      

  2.   

    --测试数据
    DECLARE @t TABLE(ID int,Name varchar(10),Birthday datetime)
    INSERT @t SELECT 1,'aa','1999-01-01'
    UNION ALL SELECT 2,'bb','1996-02-29'
    UNION ALL SELECT 3,'bb','1934-03-01'
    UNION ALL SELECT 4,'bb','1966-04-01'
    UNION ALL SELECT 5,'bb','1997-05-01'
    UNION ALL SELECT 6,'bb','1922-11-21'
    UNION ALL SELECT 7,'bb','1989-12-11'DECLARE @dt1 datetime,@dt2 datetime
    SELECT @dt1='2003-12-05',@dt2='2006-02-28'
    select * from @t
    where dateadd(year,datediff(year,birthday,@dt1),birthday) between @dt1 and convert(datetime,convert(char(5),@dt1,120)+'12-31')
     or  dateadd(year,datediff(year,birthday,@dt1)+1,birthday) between convert(datetime,convert(char(5),dateadd(year,1,@dt1),120)+'1-1')and @dt2
    /*思路:1、把生日的年加到和开始的日期的年相同,即与@dt1的年相同,检查是否在@dt1到该年年底之间
            2、把生日的年加到和开始的日期的下一年,即@dt1的下一年,检查是否在@dt1下一年开始到@dt2之间
    */
      

  3.   

    case when (year(birthday)%4=0 and year(birthday)%100!=0) or year(birthday)%400=0  and then '闰年' else '非闰年' end
      

  4.   

    不用吧?只需要考虑月份即可.select * from tb where convert(varchar(7),dateadd(mm,1,getdate()),120) = datename(yy,getdate()) + '-' + right('0' + datename(mm,dt),2)
      

  5.   

    你可以先声明几个参数,然后再datepart函数、dateadd函数判断是否为闰年,最后再用select case when查询出下个月过生日的员工。
      

  6.   

    下月过生日
    select *
    from tbname
    where datepart(month,getdate())+1=datepart(month,你的生日字段)
      

  7.   

    不用考虑闰年什么的create table #tb
    (
    [name] varchar(10),
    [birthday] datetime
    )insert into #tb
    select '张三','1983-09-11' union all
    select '张四','2000-02-29' union all
    select '张五','1983-03-11' union all
    select '张六','1983-04-11' union all
    select '张七','1983-03-21'select * 
    from #tb 
    where 
    datepart(mm,birthday) = datepart(mm,getdate()) + 1 and
    datepart(day,dateadd(dd,-day(getdate()),dateadd(m,2,getdate()))) - datepart(day,birthday) >= 0name       birthday
    ---------- -----------------------
    张五         1983-03-11 00:00:00.000
    张七         1983-03-21 00:00:00.000(2 行受影响)
    dateadd(dd,-day(getdate()),dateadd(m,2,getdate())) 表示下个月的最后一天,你可以看看能否优化短一些
      

  8.   

    只需要考虑月份即可.select * from tb where month(dateadd(mm,1,getdate())) = month(dt)
      

  9.   

    Inside Microsoft SQL Server 2005: T-SQL Programming 有现成的例子 
    Chapter 1: Datatype-Related Problems, XML, and CLR UDTs
    -> DATETIME Datatypes
    -> The Birthday Problem
      

  10.   


    --Before you start working on a solution, run the following code, which adds two employees to the Employees table:
    USE Northwind;INSERT INTO dbo.Employees(LastName, FirstName, BirthDate)
      VALUES('Leaping', 'George', '19720229');
    INSERT INTO dbo.Employees(LastName, FirstName, BirthDate)
      VALUES('Today', 'Mary', CAST(CONVERT(CHAR(8), GETDATE(), 112) AS DATETIME));--George Leaping was born on February 29, 1972, and Mary Today was born today. Here's the solution query:WITH Args1 AS
    (
      SELECT LastName, FirstName, BirthDate,
        DATEDIFF(year, BirthDate, GETDATE()) AS Diff,
        CAST(CONVERT(CHAR(8), GETDATE(), 112) AS DATETIME) AS Today
      FROM dbo.Employees
    ),
    Args2 AS
    (
      SELECT LastName, FirstName, BirthDate, Today,
        DATEADD(year, Diff, BirthDate) AS BDCur,
        DATEADD(year, Diff + 1, BirthDate) AS BDNxt
      FROM Args1
    ),
    Args3 AS
    (
      SELECT LastName, FirstName, 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 Args2
    )
    SELECT LastName, FirstName, BirthDate,
      CASE WHEN BDCur >= Today THEN BDCur ELSE BDNxt END AS BirthDay
    FROM Args3;
      

  11.   

    .生日问题这里只考虑特殊问题 使用DATEADD函数 为某年的2月29日加1年,下年由于不是闰年,所以系统默认是XXXX年2月28日。很多人在非闰年庆祝2月29生日 都放在3月1日(书上所说,HOHO)例子:我们来求每个员工离今天最近的生日。如果过了生日今天 那么返回明年的生日 否则 返回今年(环境2005)SET NOCOUNT ON;WITH Args1 AS(  SELECT Name, BirthDate,    DATEDIFF(year, BirthDate, GETDATE()) AS Diff,--出生日期和今天的相差年份    CAST(CONVERT(CHAR(8), GETDATE(), 112) AS DATETIME) AS Today --今天的午夜时间  FROM dbo.Employees),Args2 AS(  SELECT Name, BirthDate, Today,    DATEADD(year, Diff, BirthDate) AS BDCur,--这个是今年的生日     DATEADD(year, Diff + 1, BirthDate) AS BDNxt--这个是明年的生日  FROM Args1),Args3 AS(  SELECT LastName, FirstName, BirthDate, Today,    BDCur + CASE WHEN DAY(BirthDate) = 29 AND DAY(BDCur) = 28      THEN 1 ELSE 0 END AS BDCur,--这里需要判断如果出生的那天是闰年的月日,而今年的生日是日,那么+1天(号过生日)    BDNxt + CASE WHEN DAY(BirthDate) = 29 AND DAY(BDNxt) = 28     THEN 1 ELSE 0 END AS BDNxt ,--这里需要判断如果出生的那天是闰年的月日,而明年的生日是日,那么+1天(号过生日)  FROM Args2)SELECT Name, BirthDate,  CASE WHEN BDCur >= Today THEN BDCur ELSE BDNxt END AS BirthDay--如果今年的生日还没过(比今天晚),那么就取今年的反之取下一年FROM Args3;
      

  12.   


    想复杂啦,你认为month('2012-2-29')等于几?
      

  13.   


    参看我的9#,没有那么多条件要判断的。
    用函数
    dateadd(dd,-day(getdate()),dateadd(m,2,getdate())) 
    取出下个月的最后一天,比如下个月是2月闰年,那自然会返回29,如果不是闰年,返回28
    系统会自动判断地。你只要确定
    1.生日的月份相同。
    2.生日的天数小于等于下个月的最后一天。