34,593
社区成员
发帖
与我相关
我的任务
分享
where id=2 and StartDate>='2011-03-01' and EndDate<='2011-03-15'
--这只不过就是比较符号的方向问题
select id,StarDate,EndDate from
tb where id = 1 and StarDate between '2011/03/08' and '2011/03/09'
select id,StarDate,EndDate from
tb where id = 2 and StarDate between '2011/03/01' and '2011/03/15'
use test
go
if object_id('test.dbo.tb') is not null drop table tb
-- 创建数据表
create table tb
(
id int,
StartDate datetime,
EndDate datetime
)
go
--插入测试数据
insert into tb select 1,'2011-03-01','2011-03-15'
union all select 2,'2011-03-08','2011-03-09'
union all select 3,'2011-04-02','2011-04-20'
union all select 1,'2011-03-01','2011-03-23'
union all select 2,'2011-02-23','2011-03-18'
go
--代码实现
select id,StartDate=convert(varchar(10),StartDate,120)
,EndDate=convert(varchar(10),EndDate,120)
from tb where id=1 and StartDate<'2011-03-08' and EndDate>'2011-03-09'
/*测试结果
id StartDate EndDate
-------------------------------
1 2011-03-01 2011-03-15
1 2011-03-01 2011-03-23
(2 行受影响)
*/
select id,StartDate=convert(varchar(10),StartDate,120)
,EndDate=convert(varchar(10),EndDate,120)
from tb where id=2 and StartDate<'2011-03-01' and EndDate>'2011-03-15'
/*测试结果
id StartDate EndDate
-------------------------------
2 2011-02-23 2011-03-18
(1 行受影响)
*/