2016-12-02 48 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   ║ 
╚═══════════════╩═══════════════╩══════════════╝