一个表中有一日期型字段.如:
name          birthday
---------     ------------
aaa           1981-11-2
bbb           1980-1-20
ccc           1981-10-30
eee          1987-2-2
....怎么取出5位最近要过生日的人的名字?

解决方案 »

  1.   

    select top 5 * from table where DATEPART(month, birthday)=DATEPART(month, GETDATE()) and DATEPART(day, birthday)<=DATEPART(day, GETDATE())
      

  2.   

    改一下
    select top 5 * from table where DATEPART(month, birthday)>=DATEPART(month, GETDATE()) and DATEPART(day, birthday)>=DATEPART(day, GETDATE()) order by DATEPART(month, birthday),DATEPART(day, birthday) 
      

  3.   

    什么数据库?如果是sql server
    select top 5 * from
    (select name,cast( cast(year(getdate()) as char(4))+  '-' + cast(monthbirthday) as char(2))+'-'+cast( daybirthday)  as char(2)) as datetime) as dd from table) a
    where dd>getdate()
    order by dd其他的可以效仿ps楼上的逻辑错误!
      

  4.   

    什么数据库?如果是sql server
    select top 5 * from
    (select name,cast( cast(year(getdate()) as char(4))+  '-' + cast(monthbirthday) as char(2))+'-'+cast( daybirthday)  as char(2)) as datetime) as dd from table) a
    where dd>getdate()
    order by dd其他的可以效仿ps楼上的逻辑错误!
    Microsoft OLE DB Provider for SQL Server 错误 '80040e31' 超时已过期 /Expert/reply.asp,行105
      

  5.   

    ACCESS好象没有(cast)这些函数吧.
      

  6.   

    不好选,我想既然是要过生日嘛,必然>今天,所以
    select top 5 * from (select * from table where birthday>getdate() order by birthday)俺可是测试过了,可以的
      

  7.   

    TO:myboor(菜菜菜鸟) 
    用户填写的生日都是今天以前(1980,1972..等等).这样写是不行的.
      

  8.   

    oracle 数据库
    select name,birthday from
    (select t.name,t.birthday
    from tw t where birthday is not null 
    order by 
    abs(to_number(to_char(birthday,'mm'))-to_number(to_char(sysdate,'mm'))),
    abs(to_number(to_char(birthday,'dd'))-to_number(to_char(sysdate,'dd')))) a
    where rownum<5
      

  9.   

    select top 5 * from table where birthday>getdate() order by birthday