sql中两个日期之间缺少日期
我想找出两个日期之间缺少的日期。 表中有一个名为date的字段,它显示在该字段中 2018-11-132018-11-1822018-11-2022018-11-25sql中两个日期之间缺少日期,sql,sql-server,Sql,Sql Server,我想找出两个日期之间缺少的日期。 表中有一个名为date的字段,它显示在该字段中 2018-11-132018-11-1822018-11-2022018-11-25 Expected Output 2018-11-14 2018-11-15 2018-11-16 2018-11-17 2018-11-19 2018-11-21 2018-11-22 2018-11-23 2018-11-24 我想要2018-11-13到2018-11-25的缺失日期 Expected Output 2018
Expected Output
2018-11-14
2018-11-15
2018-11-16
2018-11-17
2018-11-19
2018-11-21
2018-11-22
2018-11-23
2018-11-24
我想要2018-11-13到2018-11-25的缺失日期
Expected Output
2018-11-14
2018-11-15
2018-11-16
2018-11-17
2018-11-19
2018-11-21
2018-11-22
2018-11-23
2018-11-24
如果希望在某些表中存储丢失的日期,可以使用While循环。这样它也会起作用
Declare @mindate date ='2018-11-13'
Declare @maxdate date = '2018-12-01'
select '2018-11-18' as Existingdate into #tmp --made existing dates
create table Missingdates (Missingdate date)
Declare @id int = 1
While dateadd(day, @id, @mindate) > @mindate and dateadd(day, @id, @mindate) < @maxdate
Begin
insert into Missingdates select dateadd(day, @id, @mindate) as MIssingDate where not exists (Select * from #tmp t where t.Existingdate = dateadd(day, @id, @mindate))
set @id = @id+1
end
select * from Missingdates
添加了not exists子句,它不会给出现有日期 这肯定会奏效:
select * from date_ranges;
13-NOV-18
01-DEC-18
16-NOV-18
20-NOV-18
set serveroutput on;
create or replace procedure date_range( d1 date, d2 date) is
op date;
op2 date;
diff number;
comp date;
a number;
b number;
j number;
cursor c1 is select a from date_ranges;
begin
select d2-d1 into diff from dual;
for op in 1..diff
loop <<forloop>>
j:=0;
select d1+op into op2 from dual;
open c1;
loop <<curloop>>
fetch c1 into comp;
if(c1%notfound) then
exit;
end if;
select sysdate-to_date(comp) into a from dual;
select sysdate-to_date(op2) into b from dual;
if(a=b)then
j:=1;
end if;
end loop curloop;
close c1;
if(j=0)then
DBMS_OUTPUT.PUT_LINE(op2);
end if;
end loop forloop;
end;
/
declare
m date;
n date;
begin
select min(a) into m from date_ranges;
select max(a) into n from date_ranges;
date_range(m,n);
end;
/
所以不打印11月16日和11月20日的日期,因为它们出现在查询的表中,并且缺少在示例日期的最小值和最大值之间打印的日期。
非常感谢。1您是否有任何日历日期表来查找日期范围内缺失的日期?@Avi no sir….@venkataraman它显示日期之间缺失的所有日期…我想如果在2018-11-13和2018-12-01之间,数据库中存在2018-11-18日期,则查询将跳过该日期并显示其余缺失的日期dates@Lima,请提供您的数据库模式,并更新您的问题。当前的问题有误导性。@Squirrel它不重复它是一个不同的问题,因为缺少日期不能在表中它显示日期之间缺少的所有天数…我想如果在2018-11-13和2018-12-01之间,数据库中存在日期2018-11-18,则查询将跳过该日期并显示缺少的其余日期dates@Lima这个问题将检查表中是否存在该日期,然后插入缺少的日期。这不会给出2018-11-18,并返回所有缺失的日期。它会显示日期之间缺失的所有日期…我想如果在2018-11-13和2018-12-01之间,数据库中存在2018-11-18的日期,则查询将跳过该日期并显示其余缺失的日期确定…,,,@Lima正确吗??这就是你想要的!!!!
14-NOV-18
15-NOV-18
17-NOV-18
18-NOV-18
19-NOV-18
21-NOV-18
22-NOV-18
23-NOV-18
24-NOV-18
25-NOV-18
26-NOV-18
27-NOV-18
28-NOV-18
29-NOV-18
30-NOV-18