如何在PL / SQL中将字符串解析为标记?
例如,这是我的字符串:
170823-14785
我想要的解析是:
17
08
23
-1
47
85
答案 0 :(得分:1)
正如我所见,你想要用以下空格符号替换所有连续的2个字符
如果是这样的话,那么:(不需要PL / SQL)
SELECT REGEXP_REPLACE('170823-14785', '(.{2})', '\1 ') FROM DUAL;
答案 1 :(得分:1)
假设您想将字符串拆分为每个2个字符的标记,无论字符串的内容如何,这都可以是一种方式,而不需要PL / SQL:
select substr(str, (level-1)*2 +1, 2)
from (
select '170823-14785' str
from dual
)
connect by 2*level -1 <= length(str)
order by level
给出了:
SUBSTR(S
--------
17
08
23
-1
47
85
在选择列表中添加内容将阐明其工作原理:
select substr(str, (level-1)*2 +1, 2) as token,
level,
(level-1)*2 +1 as startPos
from (
select '170823-14785' str
from dual
)
connect by 2*level -1 <= length(str)
order by level
TOKEN LEVEL STARTPOS
-------- ---------- ----------
17 1 1
08 2 3
23 3 5
-1 4 7
47 5 9
85 6 11
答案 2 :(得分:0)
使用SUBSTR( string, position, substring_length )
:
DECLARE
v_input VARCHAR2(20) := '170823-14785';
v_str1 CHAR(2);
v_str2 CHAR(2);
v_str3 CHAR(2);
v_str4 CHAR(2);
v_str5 CHAR(2);
v_str6 CHAR(2);
BEGIN
v_str1 := SUBSTR( v_input, 1, 2 );
v_str2 := SUBSTR( v_input, 3, 2 );
v_str3 := SUBSTR( v_input, 5, 2 );
v_str4 := SUBSTR( v_input, 7, 2 );
v_str5 := SUBSTR( v_input, 9, 2 );
v_str6 := SUBSTR( v_input, 11, 2 );
END;
/
答案 3 :(得分:0)
此示例使用REGEXP_SUBSTR()并通过使第二个字符可选来处理奇数个字符,并在后跟字符或字符串结尾时使2个字符的组有效。总是期待意外!
SQL> with tbl(str) as (
select '170823-147854' from dual
)
select level, regexp_substr(str, '((.)(.)?)(.|$)', (((level-1)*2)+1), 1, NULL, 1) parsed
from tbl
connect by level <= round(length(str)/2)
order by level;
LEVEL PARSED
---------- -------------
1 17
2 08
3 23
4 -1
5 47
6 85
7 4
7 rows selected.
SQL>