日期:2014-05-18 浏览次数:20698 次
--> 测试数据:[test] if object_id('[test]') is not null drop table [test] create table [test]( [workername] varchar(4), [begindate] date, [enddate] date ) go insert [test] select '张三','2012-6-1','2012-6-12' union all select '李四','2012-6-5','2012-6-20' union all select '王五','2012-6-9','2012-6-10' go declare @d1 date set @d1='2012-6-1' declare @d2 date set @d2='2012-6-8' select * from test where [begindate] between @d1 and @d2 or [enddate] between @d1 and @d2 or [begindate]<@d1 and @d2>[enddate]--若果需要的是两个区间有交集就绪把这个加上,不需要就去掉 /* workername begindate enddate ------------------------------------ 张三 2012-06-01 2012-06-12 李四 2012-06-05 2012-06-20 */
------解决方案--------------------
--更正一下 --> 测试数据:[test] if object_id('[test]') is not null drop table [test] create table [test]( [workername] varchar(4), [begindate] date, [enddate] date ) go insert [test] select '张三','2012-6-1','2012-6-12' union all select '李四','2012-6-5','2012-6-20' union all select '王五','2012-6-9','2012-6-10' go declare @d1 date set @d1='2012-6-1' declare @d2 date set @d2='2012-6-8' select * from test where [begindate] between @d1 and @d2 or [enddate] between @d1 and @d2 or ([begindate]<@d1 and @d2>[enddate])--若果需要的是两个区间有交集就绪把这个加上,不需要就去掉 /* workername begindate enddate ------------------------------------ 张三 2012-06-01 2012-06-12 李四 2012-06-05 2012-06-20 */
------解决方案--------------------
select * from test
where '2012-6-1' between [begindate] and [enddate]
or '2012-6-8' between [begindate] and [enddate]
------解决方案--------------------