select 年
,初日=stuff(right(min(年*10000+月*100+日),4),3,0,'月')+'日'
,终日=stuff(right(max(年*10000+月*100+日),4),3,0,'月')+'日'
from 表
where charindex(' 60 ',' '+w+' ')>0
group by 年

解决方案 »

  1.   

    --查询示例--示例数据
    declare @t table(年 int,月 int,日 int,w varchar(10))
    insert @t select 1959,1,1 ,'01 10'
    union all select 1959,1,2 ,'01 10'
    union all select 1959,1,3 ,'01'
    union all select 1959,1,4 ,'01 60'
    union all select 1959,1,5 ,'60'
    union all select 1959,1,6 ,'60 01'
    union all select 1959,1,7 ,'01 80'
    union all select 1959,1,8 ,'01 10'
    union all select 1959,1,9 ,'01 10'
    union all select 1959,1,10,'60'
    union all select 1959,2,1 ,'80'
    union all select 1959,2,2 ,'80 01'
    union all select 1959,2,3 ,'01 10'
    union all select 1959,2,4 ,'01 10'
    union all select 1959,2,5 ,'01'
    union all select 1959,2,6 ,'01'
    union all select 1959,2,7 ,'01'
    union all select 1959,2,8 ,'60'
    union all select 1959,2,9 ,'60'
    union all select 1959,2,10,'13 16'select 年
    ,初日=stuff(right(min(年*10000+月*100+日),4),3,0,'月')+'日'
    ,终日=stuff(right(max(年*10000+月*100+日),4),3,0,'月')+'日'
    from @t
    where charindex(' 60 ',' '+w+' ')>0
    group by 年/*--测试结果年           初日           终日           
    ----------- ------------ ------------ 
    1959        01月04日       02月09日(所影响的行数为 1 行)
    --*/
      

  2.   

    select 年,min(月) as 月,min(日) as 日,min(w) as w from tb 
    where charindex('60',w)>0 group by 年
      

  3.   

    --如果要去掉日月中多余的0,则:
    select 年
    ,初日=cast(min(年*10000+月*100+日)%10000/100 as varchar)+'月'
    +cast(min(年*10000+月*100+日)%10000%100 as varchar)+'日'
    ,终日=cast(max(年*10000+月*100+日)%10000/100 as varchar)+'月'
    +cast(max(年*10000+月*100+日)%10000%100 as varchar)+'日'
    from @t
    where charindex(' 60 ',' '+w+' ')>0
    group by 年