我想根据SQL Server中给定的输入日期范围选择记录。 手段, 我的桌子有以下记录,
ProposedStartDate, ProposedEndDate, Roomid, Childid 2016-11-01 2016-11-31 111 10001 2016-12-15 2016-12-31 121 10001 2017-01-01 2017-01-31 111 10001 2017-01-01 2017-01-31 121 10001 2017-02-01 2017-02-28 111 10001
现在,我希望在日期之间显示具有唯一标识符的记录。输入是:' 2016-12-25'和' 2017-01-15'
答案 0 :(得分:1)
根据您的要求,这应该有效
DECLARE @dates TABLE(ProposedStartDate varchar(10), ProposedEndDate varchar(10), Roomid int, Childid INT)
insert into @dates values ('2016-11-01' , '2016-11-31' , 111 , 10001)
insert into @dates values ('2016-12-15' , '2016-12-31' , 121 , 10001)
insert into @dates values ('2017-01-01' , '2017-01-31' , 111 , 10001)
insert into @dates values ('2017-01-01' , '2017-01-31' , 121 , 10001)
insert into @dates values ('2017-02-01' , '2017-02-28' , 111 , 10001)
select * from @dates
where CONVERT(datetime,ProposedStartDate)>=CONVERT(datetime, '2016-12-25' )
and CONVERT(datetime,ProposedStartDate)<=CONVERT(datetime, '2017-01-15' )
结果是:
*-----------------*-----------------*--------*-------*
|ProposedStartDate|ProposedEndDate |Roomid |Childid|
*-----------------*-----------------*--------*-------*
|2017-01-01 |2017-01-31 |111 |10001 |
*-----------------*-----------------*--------*-------*
|2017-01-01 |2017-01-31 | 121 |10001 |
*-----------------*-----------------*--------*-------*