I have a reasonably large set of phone numbers (approximately 2 million) in a database table. These numbers have been inserted in blocks, so there are many continuous ranges of numbers, anything from 10 numbers to 10 thousand in a range. Some of these numbers are in use and are therefore marked as unavailable, the rest are available. Given a particular number I need a way to find continuous ranges of numbers, both above and below that number. The range should continue until it finds an unavailable number, or encounters the boundary of two ranges.
For example given the following set:
1000
1001
1002
1010
1011
1012
1013
1020
1021
1022
Doing a search using 1012 as the parameter should return 1010, 1011, 1012, 1013.
What is a good way of forming a query to find these ranges? We use NHibernate on top of SQL server, a solution using either is fine.