如何从字符串字段中分离独立字符的实例

时间:2016-12-02 14:13:01

标签: sql database oracle

希望我能正确解释我的问题。我有一个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。另外请记住,独立字符可能并不总是在字符串的末尾。谢谢。

3 个答案:

答案 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

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

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