+-------------+
| Things |
---------------
|Structure A |
|House B |
|His Tent C |
|Her canoe 1 |
|My Big Shoe |
|My Big Shoe 7|
---------------
+-------------------------------------+
| Thingss | col 1 | col 2 |
---------------------------------------
|Structure A | Structure | A |
|House B | House | B |
|C His Tent | His Tent | C |
|Her canoe 1 | Her canoe | 1 |
|My Big Shoe | My Big Shoe | |
|My Big Shoe 7| My Big Shoe | 7 |
---------------------------------------
如果该东西没有独立的字母数字值,则它会在col 2字段中返回null。另外请记住,独立字符可能并不总是在字符串的末尾。谢谢。
答案 0 :(得分:4)
提取一个独立的角色
select Things
,regexp_replace(Things,'(^| )(.)( |$)','\1\3') as col1
,regexp_substr (Things,'(^| )(.)( |$)',1,1,null,2) as col2
from t
;
提取罗马数字
select Things
,regexp_replace(Things,'(^| )([IVX]+)( |$)','\1\3') as col1
,regexp_substr (Things,'(^| )([IVX]+)( |$)',1,1,null,2) as col2
from t
;
答案 1 :(得分:1)
您应该使用Oracle REGEXP_
函数。
此查询还处理字符串中间和开头中的字母和数字:
SELECT Things,
Trim(REGEXP_REPLACE(Things, '(\s|^)(\d|\S)(\s|$)',' ')) as Col1,
Trim(REGEXP_SUBSTR(Things, '(\s|^)(\d|\S)(\s|$)')) as Col2
FROM Table1
INSERT ALL
INTO Table1 (Things)
VALUES ('A Structure')
INTO Table1 (Things)
VALUES ('Structure A')
INTO Table1 (Things)
VALUES ('House B')
INTO Table1 (Things)
VALUES ('His Tent C')
INTO Table1 (Things)
VALUES ('Her canoe 1')
INTO Table1 (Things)
VALUES ('My Big 8 Shoe')
INTO Table1 (Things)
VALUES ('My Big Shoe 7')
SELECT * FROM dual
\\
SELECT Things,
Trim(REGEXP_REPLACE(Things, '(\s|^)(\d|\S)(\s|$)',' ')) as Col1,
Trim(REGEXP_SUBSTR(Things, '(\s|^)(\d|\S)(\s|$)')) as Col2
FROM Table1
\\
RESULT
╔═══════════════╦═══════════════╦══════════════╗
║ THINGS ║ Col1 ║ Col2 ║
╠═══════════════╬═══════════════╬══════════════╣
║ A Structure ║ Structure ║ A ║
║ Structure A ║ Structure ║ A ║
║ House B ║ House ║ B ║
║ His Tent C ║ His Tent ║ C ║
║ Her canoe 1 ║ Her canoe ║ 1 ║
║ My Big 8 Shoe ║ My Big Shoe ║ 8 ║
║ My Big Shoe 7 ║ My Big Shoe ║ 7 ║
╚═══════════════╩═══════════════╩══════════════╝
答案 2 :(得分:0)
可以使用INSTR
SELECT
Things,
CASE
WHEN length(SUBSTR(Things, INSTR(Things,' ',-1) + 1)) = 1
then SUBSTR(Things,1,INSTR(Things,' ',-1))
else Things
end col1,
CASE
WHEN length(SUBSTR(Things, INSTR(Things,' ',-1) + 1)) = 1
then SUBSTR(Things, INSTR(Things,' ',-1) + 1)
else NULL
end col2
FROM my_table