当前位置: 代码迷 >> Sql Server >> 求一段查询时间段解决方案
  详细解决方案

求一段查询时间段解决方案

热度:84   发布时间:2016-04-27 11:29:50.0
求一段查询时间段
比如我数据库时有数据
开始时间 结束时间
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 @Tselect '2012-08-15 09:00:00','2012-08-15 10:00:00' union allselect '2012-08-16 09:00:00','2012-08-16 10:00:00' union allselect '2012-08-15 10:00:00','2012-08-15 11:00:00' union allselect '2012-08-15 11:00:00','2012-08-15 12:00:00'select * from @Twhere datepart(hh,开始时间)=9 and (datepart(hh,结束时间) in (9,10))/*开始时间                    结束时间----------------------- -----------------------2012-08-15 09:00:00.000 2012-08-15 10:00:00.0002012-08-16 09:00:00.000 2012-08-16 10:00:00.000*/
  相关解决方案