比如我数据库时有数据
开始时间                   结束时间
2012-08-15 09:00:00        2012-08-15 10:00:00  
2012-08-16 09:00:00        2012-08-16 10:00:00 
2012-08-15 10:00:00        2012-08-15 11:00:00 
2012-08-15 11:00:00        2012-08-15 12:00:00 
我想要查询每天9点到10点的数据`
就像第一条与每二条符合`
要怎么写

解决方案 »

  1.   


    declare @T table([开始时间] datetime,[结束时间] datetime)
    insert @T
    select '2012-08-15 09:00:00','2012-08-15 10:00:00' union all
    select '2012-08-16 09:00:00','2012-08-16 10:00:00' union all
    select '2012-08-15 10:00:00','2012-08-15 11:00:00' union all
    select '2012-08-15 11:00:00','2012-08-15 12:00:00'select * from @T
    where datepart(hh,开始时间)=9 and (datepart(hh,结束时间) in (9,10))
    /*
    开始时间                    结束时间
    ----------------------- -----------------------
    2012-08-15 09:00:00.000 2012-08-15 10:00:00.000
    2012-08-16 09:00:00.000 2012-08-16 10:00:00.000
    */
      

  2.   

    如果有9点20到9点40的数据,是不是也算是9点到10点之间的数据?--我上面写的是测试数据,你不用管数据的多少select * from 你的表名
    where datepart(hh,开始时间)=9 and datepart(hh,结束时间)=10