SQL: Find missing numbers in sequence

WannaFly

Platinum Member
Jan 14, 2003
2,811
1
0
I've googled it a bit, can cant seem to find a solution that doesnt use a temp table or cursor. I need a SQL statement that would return numbers that are missing from a range of numbers.

Say, range 1-10

Table1:
--------
1
2
4
5
6
8
9
10

I'd want my query to return 3 and 7.
I think i might just have to create a non-temporary table and fill it with values 0-1000000 and use a NOT EXISTS clause. (I'll be running this query frequently, hence not using a temp table)
 

Gunslinger08

Lifer
Nov 18, 2001
13,234
2
81
The attached code will select the ranges of missing numbers, up to the highest remaining number in the list. I'm sure you can figure it out from there.

Replace "number" with the field you're looking at. Replace t_Num with the table name.