子查询select * from (select a,b,c, d=表达式 from tb ...) where d between ...

解决方案 »

  1.   

    create table tb(出生年月 datetime)
    insert into tb values('1930-01-01')
    insert into tb values('1980-12-31')
    insert into tb values('1960-07-01')
    insert into tb values('1969-12-07')
    insert into tb values('2000-12-06')
    insert into tb values('2000-12-08')
    goselect * , nl = 
      case when right(convert(varchar(10),出生年月,120),5) >= right(convert(varchar(10),getdate(),120),5) then datediff(yy,出生年月,getdate()) -1 
           else datediff(yy,出生年月,getdate())
      end
    from tbdrop table tb/*
    出生年月                                                   nl          
    ------------------------------------------------------ ----------- 
    1930-01-01 00:00:00.000                                79
    1980-12-31 00:00:00.000                                28
    1960-07-01 00:00:00.000                                49
    1969-12-07 00:00:00.000                                39
    2000-12-06 00:00:00.000                                9
    2000-12-08 00:00:00.000                                8(所影响的行数为 6 行)
    */
      

  2.   

    我觉得从效率来看的话,并没问题若觉得代码太多的话!我觉得可以考虑写个自定义函数。。create function ....return