TABLE上面有字段YY,MM(出生年月)
现在要找25周岁
25周岁-35周岁
35周岁-45周岁
45周岁以上?

解决方案 »

  1.   

    25周岁以下
    select * from tablename
    where YY>year(getdate())-25 or YY=year(getdate())-25 and MM>month(getdate()) 25周岁-35周岁
    select * from tablename
    where (YY>year(getdate())-35 or YY=year(getdate())-35 and MM>month(getdate()))
    and (YY<year(getdate())-25 or YY=year(getdate())-25 and MM<=month(getdate()))--其他类似
      

  2.   

    CREATE TABLE #T(YY INT,MM INT)
    INSERT #T SELECT '1976','10' UNION 
    SELECT '1965','1' union 
    select '1956','2'  
    --25周岁
    SELECT * from  #t where (YEAR(GETDATE())-yy)=25 and month(getdate())<mm
    --25-35
    SELECT * from  #t where (YEAR(GETDATE())-yy)>25 and (YEAR(GETDATE())-yy)<35
    --35-45
    SELECT * from  #t where (YEAR(GETDATE())-yy)>35 and (YEAR(GETDATE())-yy)<=45
    --45-55
    SELECT * from  #t where (YEAR(GETDATE())-yy)>45 and (YEAR(GETDATE())-yy)<=55
      

  3.   

    create table t
    (id int identity(1,1),
    yy1 int,
    mm1 int
    )insert into t values(1982,2)
    select 1983,7 union all
    select 1984,6 union all
    select 1970,1 union all
    select 1954,5 --25
    select * from t
    where year(getdate())-yy1 =25 and mm1<=month(getdate())
    --25-35
    select * from t
    where year(getdate())-yy1 between 25 and 35 and mm1<=month(getdate())--大於45
    select * from t
    where year(getdate())-yy1 >45 and mm1<=month(getdate())
      

  4.   

    id          yy1         mm1         
    ----------- ----------- ----------- 
    5           1982        2(1 row(s) affected)id          yy1         mm1         
    ----------- ----------- ----------- 
    5           1982        2(1 row(s) affected)id          yy1         mm1         
    ----------- ----------- ----------- 
    4           1954        5(1 row(s) affected)