日期:2014-05-17  浏览次数:20432 次

求一段查询时间段
比如我数据库时有数据
开始时间 结束时间
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点的数据`
就像第一条与每二条符合`
要怎么写

------解决方案--------------------
SQL code

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
*/