Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Searching Missing Number

Author  Topic 

samamun001
Starting Member

17 Posts

Posted - 2005-01-09 : 09:23:28
Hello,
I have a table say temp. In temp the info is like

1
2
5
7

I want the missing number.

3
4
6

How can I do it without using cursor

samamun

amachanic
SQL Server MVP

169 Posts

Posted - 2005-01-09 : 09:33:23
First create a table of numbers:

http://sqljunkies.com/WebLog/amachanic/articles/NumbersTable.aspx

Then:

SELECT N.Number
FROM Numbers N
LEFT JOIN YourTable T
ON T.Number = N.Number
WHERE T.Number IS NULL
Go to Top of Page

amachanic
SQL Server MVP

169 Posts

Posted - 2005-01-09 : 09:34:52
In retrospect, you may want to change it slightly:

SELECT N.Number
FROM Numbers N
LEFT JOIN YourTable T
ON T.Number = N.Number
WHERE T.Number IS NULL
AND N.Number < (SELECT MAX(T1.Number) FROM YourTable T1)
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2005-01-09 : 10:22:57
see
http://www.mindsdoor.net/SQLTsql/FindGapsInSequence.html



==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page
   

- Advertisement -