I'm looking for a cursor free solution compatible with SQL2005 to return start and end values of the range found or just a start value for a single value. Thanks.
I suspect a WITH CTE and perhaps a windowing function are the key to this, but I'm still new to them.
Here's a sample list and what I'd like to achieve:
SET NOCOUNT ON
CREATE TABLE #lists ( [id] [int] NOT NULL CONSTRAINT [PK_lists] PRIMARY KEY CLUSTERED ( [id] ASC ) )
CREATE TABLE #expectedoutput ( [start] [int] NOT NULL, [end] [int] NULL )
insert into #lists select 1
insert into #lists select 2
insert into #lists select 3
insert into #lists select 5
insert into #lists select 6
insert into #lists select 9
insert into #lists select 12
insert into #expectedoutput select 1,3
insert into #expectedoutput select 5,6
insert into #expectedoutput select 9,null
insert into #expectedoutput select 12,null
select * from #lists
select * from #expectedoutput
drop table #lists
drop table #expectedoutput