您的位置:首页 > 其它

趣味的字符配对,CSDN网友问题

2009-11-12 16:58 274 查看
--question

问题是这样的,我得到一个字符串,3c,有两个栏位里有如下信息3a,3b,3c,11,12,13,4a,4b,4c,另一个栏位里是1,2,3,4,5,6,7,8,9
也就是说通过字符串3c找到栏位一里3c对应的,号在第几个位置上,然后在第二个栏位里取该位置,号前的字符串,我想了一下用instr取到的位置不准,不知道应该怎么弄,大侠指点一下!!

--solute:

如是两位数的话,是有规律的!
可以用以下的操作:

select distinct a,trunc((b/3)+1) b from
(
select substr(a,mod(rownum,3)*rownum,2) a,
mod(rownum,3)*rownum b from
(
select '3a,3b,3c,11,12,13,4a,4b,4c' a from dual
)
connect by rownum <length(a)
) where b <>0 and a is not null
--and a='3c'
order by b

--result:

3a 1
3b 2
3c 3
11 4
12 5
13 6
4a 7
4b 8
4c 9

--other method:

写了一个不管中间有多少个字符的,位数不固定的!

select case when c=0 then substr(a,b+1,length(a)-1) else substr(a,b+1,c-b-1) end b,rownum from
(
select a,b,
nvl(lead(b) over(partition by a order by b),0) c,
lag(b) over(partition by a order by b) d
from
(
select distinct a, instr(a,',',rownum) b
from
(
select '3a,3b3,3c,11,12,11111113,4a111,114b,4c' a from dual
)
connect by rownum <length(a)
)
)

--result:

3a 1
3b3 2
3c 3
11 4
12 5
11111113 6
4a111 7
114b 8
4c 9
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: