2016-12-02 36 views
3

希望我能正确解释我的问题。我有一个varchar2字段,有一个事物的描述。有些东西根据独立的字母/数字附有描述符。我的问题是,如何从主文本字段中分离单独的字母/数字实例?我提供了一个我正在寻找的简单例子。如何从字符串字段中分离独立字符的实例

+-------------+ 
| 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

你可以有一个以上一个独立的角色?如果是这样,那么你会在那种情况下挑选哪一个? –

+0

我有和vkp一样的问题。另外,你想用我的Big Shoe 11做什么? 11是一个“独立角色”?显然,它不是一个单一的角色,而只是确保在你已经有了一些答案之后你不需要改变问题。 – mathguy

+0

而且,据推测,“独立角色”也可能在字符串的中间,不仅在开始或结束时,对吗? – mathguy

回答

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 
; 
+0

这是一个很好的答案 – scaisEdge

+0

我想这个查询不能处理独立的字母在中间和字符串的开始? – valex

+0

@valex,你需要这样的解决方案吗? –

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 
+0

我收到'ORA-00923:FROM关键字找不到预期的位置'错误。有任何想法吗? – user7002207

+1

@ user7002207,我已经修复了代码,请检查您是否使用了正确的版本。 –

+0

@DuduMarkovitz感谢您的修复.. – scaisEdge

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 

Test Example

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 
\\ 

结果

╔═══════════════╦═══════════════╦══════════════╗ 
║ 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   ║ 
╚═══════════════╩═══════════════╩══════════════╝