我选择条件只有月和日起始值和截止日,现在要查询出每年在在这个时间的内的记录例如:
   1        2007-02-01
   2        2005-09-15
   3        2001-01-01结果:
   1        2007-02-01
这样我要的结果是如果查询1月5号到2月5号的就只有记录1.

解决方案 »

  1.   

    select * from tb where right(convert(varchar(10),日期,120),5) between '01-05' and '02-05'
      

  2.   

    create table tb(id int, rq datetime)
    insert into tb values(1,                 '2007-02-01') 
    insert into tb values(2,                 '2005-09-15') 
    insert into tb values(3,                 '2001-01-01') 
    goselect * from tb where right(convert(varchar(10),rq,120),5) between '01-05' and '02-05'drop table tb/*
    id          rq                                                     
    ----------- ------------------------------------------------------ 
    1           2007-02-01 00:00:00.000(所影响的行数为 1 行)
    */
      

  3.   

    declare @t table(id int,date char(10))
    insert into @t
    select 1,'2007-02-01'
    union select 2,'2005-09-15'
    union select 3,'2001-01-01'
    select * from @t where substring(date,6,2)+substring(date,9,2) between '0105' and '0205'
      

  4.   

    declare @t table(id int,dt datetime)
    insert into @t select 1,'2007-02-01'
    insert into @t select 2,'2005-09-15'
    insert into @t select 3,'2001-01-01'
    select * from @t 
    where dt between ltrim(year(dt))+'-01-05' and ltrim(year(dt))+'-02-05'
      

  5.   

    select * from tb where CreateTime  between year(CreateTime) + '-01-05' and year(CreateTime) + '-02-05'
      

  6.   

    不过也有个问题,如果我参数不是01-05而是1-5就查不出来
    select * from tb where ltrim(str(datepart(month,rq)))+'-'+ltrim(str(datepart(day,rq))) between '1-5'and '2-5'
    不知道这样可行吗?:)
      

  7.   

    select   *   from   tb   where   rtrim(str(datepart(month,rq)))+'-'+ltrim(str(datepart(day,rq)))   between   '1-5'and   '2-5' 
      

  8.   

    select   *   from   tb   where   rtrim(str(datepart(month,rq)))+'-'+rtrim(str(datepart(day,rq)))   between   '1-5'and   '2-5'