问题描述:
  表t中有2个字段y和m,分别代表 年 和 月。
  现在要求把2004年3月 至 2006年7月的数据查找出来。  请各位大侠帮一下。谢谢。

解决方案 »

  1.   

    where y=2003 and m>=3
    or y>=2004 and y<=2005
    or y=2006 and m<=7
      

  2.   

    select * from T
    where (y=2004 and m>=3) 
    or (y=2005)
    or (y=2006 and m<=7)
    ---
    select * from T
    where convert(datetime,convert(varchar,y)+'-'+convert(varchar,m)+'-01')  between '2004-03-01' and '2006-07-01'
      

  3.   

    declare @t table (y int,m int)
    insert @t select 2004,2
    union all
    select 2004,3
    union all
    select 2004,5
    union all
    select 2004,6
    union all
    select 2004,7
    union all
    select 2004,4
    union all
    select 2004,5
    select * from @t where convert(varchar(4),y)+right('00'+convert(varchar(2),m),2) between '200403' and '200407'
      

  4.   

    select * from T where convert(datatime,convert(varchar(4),y)+'-'convert(Varchar(2),m)+'-01') as ym and ym>='2004-03-01' and ym<='2006-07-31'