Wednesday 13 February 2019

How to Find Missing Values in a Sequence With SQL Server




create table sequence (
    id int not null primary key
);

insert into sequence(id) values
    (1), (2), (3), (4), (6), (7), (8), (9),
    (10), (15), (16), (17), (18), (19), (20);

Find Missing Sequence No in table

select l.id + 1 as start
from sequence as l
  left outer join sequence as r on l.id + 1 = r.id
where r.id is null;


OUTPUT-

start
5
11
21