表结构里面有YY和MM两个字段表示年和月
要查找YY和MM在输入年月之内的条件如输入2002-9 到 2007-8
而YY和MM为2007-5
结果能找到那么where子句怎么写?

解决方案 »

  1.   


    select * from 表 where YY+'-'+MM between '2002-9' and '2007-8'
      

  2.   

    create table dt(YY int,MM int)
    insert dt select 2002,9
    union all select 2007,5
    union all select 2007,8select YY,MM from dt where
    cast(YY as varchar)+'-'+cast(MM as varchar)>'2002-9' and cast(YY as varchar)+'-'+cast(MM as varchar)<'2007-8'YY          MM          
    ----------- ----------- 
    2007        5
      

  3.   

    select YY,MM from dt where
    cast(YY as varchar(4))+'-'+cast(MM as varchar(2))>'2002-9' and cast(YY as varchar(4))+'-'+cast(MM as varchar(2))<'2007-8'
      

  4.   

    首先:YY , MM为字符型字段
    那么:
    select * from 表 where YY+'-'+MM between '2002-9' and '2007-8'
      

  5.   

    两个字段是字符型吗?如果是的话:select * from 表 where YY+'-'+MM between '2002-9' and '2007-8'
    如果是数值型的,比较麻烦.要转换一下.