有下面的一个字符串,被分为两行:
declare @str varchar(100)
set @str='|01|09|02|10|03|11|04|12|'+char(10)+'|05|13|06|14|07|15|08|16|'
select @str
--结果
--|01|09|02|10|03|11|04|12|
--|05|13|06|14|07|15|08|16| 
现在我给一个数比如declare @s varchar(2) set @s='10'
我首先找到10的位置,然后我从10后的那个数开始数,数10个数
(说明:第一行数完以后转第二行,第二行完了从第一行第一个数开始接着数)
03-11-04-12-05-13-06-14-07-15
这次数到15,那么我把15跟他对应的在同一列的那个数,也就是11一同取出,结果:11,15再举个例子,比如我set @s='07',那么先找到07,从07后第一位开始数7个数
15-08-16-01-09-02-10,这里数到10,就取出10以及10所在列14两个值,结果:10,14不知道怎么编程用数据库实现。这些数字的位置是固定的。

解决方案 »

  1.   

    程序做   SQL写这个东西你不觉得有点麻烦吗?而且效率什么的......
      

  2.   

    declare @str varchar(100)
    set @str='|01|09|02|10|03|11|04|12|'+char(10)+'|05|13|06|14|07|15|08|16|'
    --select @strdeclare @s varchar(2) set @s='07'DECLARE @t TABLE(id INT IDENTITY,num VARCHAR(10))
    INSERT INTO @t --此处楼主在存储过程中去处理吧
    VALUES ('01'),('09'),('02'),('10'),('03'),('11'),('04'),('12')
    ,('05'),('13'),('06'),('14'),('07'),('15'),('08'),('16')--SELECT * FROM @t
    DECLARE @i INT
    SELECT  @i=(id+CAST(@s AS INT)) % 16  FROM @t WHERE num=@sSELECT @iSELECT num FROM @t
    WHERE id=@i OR id=@i+8
      

  3.   

    位置什么的 是固定的  把他看作临时表的数据可以不?
    --不知道怎么编程用数据库实现。这些数字的位置是固定的。
    Declare @tmptable table (tid int,tval1 char(5),tval2 char(5))
    insert @tmptable
    select 1,'01','05' union all
    select 2,'09','13' union all
    select 3,'02','06' union all
    select 4,'10','14' union all
    select 5,'03','07' union all
    select 6,'11','15' union all
    select 7,'04','08' union all
    select 8,'12','16'select * from @tmptable declare @s varchar(2) 
    set @s='10'
    set @s='07'
    set @s='02'select tt.tval1,tt.tval2
    from @tmptable tt
    where  tt.tid= (
    select DataYouWant= case  when (t.tid+@s-8)>0 then  (t.tid+@s-8) else (t.tid+@s) end
    from @tmptable t
    where t.tval1=@s or t.tval2=@s

    /*
    11,15
    10,14
    03,07
    */
      

  4.   


    declare @str3 varchar(100)
    set @str3='|01|09|02|10|03|11|04|12'+char(10)+'|05|13|06|14|07|15|08|16|'
    select @str3/*
    --首先编制好正确的结果
    01: 09,13 02: 03,07 03: 12,16 04: 06,02
    05: 15,11 06: 01,05 07: 10,14 08: 04,08
    09: 06,02 10: 15,11 11: 01,05 12: 10,14
    13: 04,08 14: 13,09 15: 07,03 16: 16,12
    */
    declare @s varchar(2) 
    set @s='01'
    while convert(int,@s)<=16
    begin
    declare @str varchar(100)
    set @str='|01|09|02|10|03|11|04|12|05|13|06|14|07|15|08|16'
    declare @s1 varchar(2),@s2 varchar(2)
    select @s1=case when patindex('%'+@s+'%',@str)+convert(int,@s)*3<len(@str)
                    then substring(@str,patindex('%'+@s+'%',@str)+convert(int,@s)*3,2)
                    else substring(@str,patindex('%'+@s+'%',@str)+convert(int,@s)*3-len(@str),2)
               end
    select @s2=case when patindex('%'+@s1+'%',@str)>24 
                    then substring(@str,patindex('%'+@s1+'%',@str)-24,2)
                    else substring(@str,patindex('%'+@s1+'%',@str)+24,2) 
               end
    select @s as '字符',@s1+','+@s2 as '结果'
    set @s=right('0'+convert(varchar(2),convert(int,@s)+1),2)
    end
    /*
    ---------------------------------
    |01|09|02|10|03|11|04|12
    |05|13|06|14|07|15|08|16|字符   结果
    ---- -----
    01   09,13
    02   03,07
    03   12,16
    04   06,02
    05   15,11
    06   01,05
    07   10,14
    08   04,08
    09   06,02
    10   15,11
    11   01,05
    12   10,14
    13   04,08
    14   13,09
    15   07,03
    16   16,12
    */--结果正确
    不知道还有没有办法,谢谢。
      

  5.   


    表1表2表3
    表3是我们要使用的表
     declare @key int
     set @key = 10 select 结果
     from T3
     where 序号 = 
     (select 序号
     from T3
     where 字符 = @key and 序号 >=16) + @key
      

  6.   

    手抖了一下 把 应该是  where 字符 = @key and 序号 <=16) + @key