select * from 表 where  convert(varchar(10),b,120)>=convert(varchar(10),开始日期,120) and convert(varchar(10),b,120)<=convert(varchar(10),结束日期,120)

解决方案 »

  1.   

    select * from 表 where  cast(开始日期 as datetime)< cast('2005-6-1' as datetime) and 
    cast(结束日期 as datetime)> cast('2005-6-1' as datetime)
      

  2.   

    --建立测试环境
    --目标
    /*
     比如我输入2005-6-1,数据库能查出002,003这两个记录,
    因为2005-6-1 在002和003这两条记录的时间段内,请问,这样的sql语句我该怎么写?
    */Create Table 表(编号 varchar(10),开始日期 varchar(10),结束日期 varchar(10))
    --插入数据
    insert into 表
    select '001','2005-1-1','2005-3-1' union
    select '002','2005-4-15','2005-7-15' union
    select '003','2005-5-16','2005-8-16' union
    select '004','2005-7-19','2005-10-19'--测试语句declare @dtmDateTime datetime
    set @dtmDateTime='2005-6-1'
    select 编号 from 表 where 开始日期<@dtmDateTime and 结束日期>@dtmDateTime
     
    --删除测试环境
    Drop Table 表
      

  3.   

    select * from 表 where  datediff(d,开始日期 ,'2005-6-1') >=0 and dateiff(d,结束日期 ,'2005-6-1') <= 0
      

  4.   

    select * from 表 where '2005-6-1' between 开始日期 and 结束日期