急!查询指定日期内的记录
--新建测试数据
create table #table (date datetime,value varchar(10))
insert into #table
select '2013-05-20','10' union
select '2013-05-22','11'
--查询
select * from #table
--如果查询范围是2013-05-20到2013-05-23这个日期范围,希望查询结果是这样的
[解决办法]
declare @start datetime,@end datetime,@index int
if OBJECT_ID('#t') is not null
drop table #t
create table #t
(
[date] datetime
)
set @start='2013-05-20';
set @end='2013-05-23';
set @index=0;
while DATEADD(day,@index,@start)<=@end
begin
insert into #t values(DATEADD(day,@index,@start));
set @index=@index+1;
end
select t1.date,case when t2.value is null then 0 else t2.value end value
from
#t t1 left join #table t2
on t1.date=t2.date