declare @stDate varchar(8),@enDate varchar(8)
select @stDate='2005/08',@enDate='2006/01'select *
from yourtable
where [year] between convert(int,left(@stDate,4)) and convert(int,left(@enDate,4))
and [month] beteen convert(int,right(@stDate,2)) and convert(int,right(@enDate,2))

解决方案 »

  1.   

    where year * 100 + month between 200508 and 200601
      

  2.   

    select * from tb where year=2005 and month>=8
    union
    select * from tb where year=2006 and month<=1
    ---or
    select * from tb where rtrim(year)+'/'+rtrim(right('0'+rtrim(month),2) between '2005/08' and '2006/01'
      

  3.   

    如果year,month做了索引的话,转成字符之后,还能用到索引吗?
      

  4.   

    谢谢各位前辈的支持。to mislrb(上班看看早报,上上CSDN,下班看看电影):
    我希望是一条SQL语句,不想用存储过程,谢谢。to zxkid(没有人会像我这样...):
    你这种方法是最最简单的方法,非常感谢。
    另外,顺便问一句,你以前是不是也遇到这种问题呀?否则,怎么能一下子就想出这种方法呢?高手呀。to wangdehao(找找找):
    谢谢你,你的方法也不错,跟楼上的类似,但不如楼上的最直接。另外,还有个小问题:
    2005、8、2006、1,这四个数字是外面传进来的,所以在 between 200508 and 200601时,我怎么拼起来比较好一点呢?
      

  5.   

    是不是要在程序里就先拼起来,再传进去?
    如果用SQL语句来拼,大概是怎么拼呢?
      

  6.   

    夸张 这么简单的问题我现在的工作,一条Select语句保存成文本(*.sql)都有10多K,呵呵
      

  7.   

    这个也不是什么存储过程吧,只是方便问题好说明而已,你可以将SELECT语句里的变量换成你
    传进的值即可啊?
    不过有点错,改下declare @stDate varchar(8),@enDate varchar(8)
    select @stDate='2005/08',@enDate='2006/01'select *
    from yourtable
    where ([year] = convert(int,left(@stDate,4)) and [month]>=convert(int,right(@stDate,2))) or ([year] = convert(int,left(@enDate,4)) and [month]<=convert(int,right(@enDate,2)))
      

  8.   

    各位不愧是前辈,太PF你们了。我从来没写过3K以上的SQL语句呢