请教如何求断号问题
在数据库一表中有如下字段,如
TABLE 中字段T1
T1的值为:
1,2,3,4,5,6,7,8,9,11,12,13,14,20,21,22
请问怎么把它查询成:1~9,11~14,20~22三段呢?

解决方案 »

  1.   

    最佳答案带数据
    if object_id('test')is not null
     drop table test
    go
    create table test(tstID int not null)
    go
    declare @i int
    set @i = 1
    declare MyCur cursor for
      select GetDate()
    open MyCur
    fetch next from MyCur
    while   @i <= 22
    begin
    if @i not in (10,15,16,17,18,19)
    insert into test values(@i)
    set @i = @i + 1
    end
    close MyCur 
    deallocate MyCur
    go
    create table MyTemp(sID int,eID int)
    declare @i int
    set @i = 1
    declare @j int
    select * from test
    declare Cur cursor for
    select tstID +1 from test where tstID+1 Not in(select tstID from test)
    open Cur
    Fetch next from Cur into @j
    while @@Fetch_Status = 0
    begin
         insert into MyTemp values(@i,@j-1)
    set @i = @j+1
    Fetch next from Cur into @j
    end
    close Cur
    deallocate Cur
    select * from MyTemp
    drop table MyTemp