BP,
Thanks for the suggestion, I'll try to remember to post more
information next time, it all made perfect sense to me!
Anyway, I am using SQL 2000. I actually found a post from Umachandar
Jayachandran in the microsoft.public.sqlserver.programming group with
the following suggestion for a similar problem someone had back in
2000:
(that message can be accessed here:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&selm=%23v%24%23efZ...)
select a.seq
from tbl a
where not exists(select * from tbl b
where b.seq - a.seq = 1 ) and
a.seq <> ( select max( seq ) from tbl )
This gives all the records that are followed by a gap. I tweaked this
so I could search between a start and end value and, since my ID
column is varchar, but I know that the right 4 characters are always
numbers, my complete code looks something like this:
CREATE TABLE Widgets
(WidgetNumber int,
WidgetID varchar(50))
GO
insert into Widgets values (1, 'A0001')
insert into Widgets values (1, 'A0002')
insert into Widgets values (1, 'A0003')
insert into Widgets values (1, 'A0004')
insert into Widgets values (1, 'A0006')
insert into Widgets values (1, 'A0007')
insert into Widgets values (1, 'A0008')
insert into Widgets values (1, 'A0009')
go
varchar(50) output
AS
FROM Widgets A
WHERE Not Exists(SELECT * FROM Widgets B WHERE
convert(int,right(B.WidgetID,4)) - convert(int,right(A.WidgetID,4)) =
1
A.WidgetID <> ( SELECT max( WidgetID ) FROM Widgets ) And
ORDER BY A.WidgetID
GO
go
The results are:
--------------------------------------------------
A0004
(1 row(s) affected)
I can now use this as the basis to find a series of complete sequences
until I get X widgets and their sequences. Basically, our customer
ships out the widgets and needs to keep track of the IDs that were
sent. Rather than a verbose listing (since they send out thousands at
a time), this allows them to get a list of the sequences of WidgetIDs
that were sent, so if they send out 10 widgets, they can get
A0001 - A0004
A0006 - A0011
This is a simplification, but the general idea is there.
Anyway, thanks again, hope this helps someone else down the line!