使用(/)字符

时间:2017-10-16 11:53:12

标签: sql oracle oracle9i

  select OVR_COMMENT, 
 substr( OVR_COMMENT, 1, instr(OVR_COMMENT,'/')-1 ) Time, 
 substr( OVR_COMMENT, instr(OVR_COMMENT,'/')+1, instr(OVR_COMMENT,'/',1,2)-instr(OVR_COMMENT,'/')-1 ) Reason, 
 substr( OVR_COMMENT, instr(OVR_COMMENT,'/',1,3)+1 )  
 from  override  where wbu_name ='RVR'

Original_column是

IVR时间= 20130409200450CST /原因= FUL // Desc = TOD_FULL_DAY

我的结果是

ROW | Val1                   | Val2              | Val3         | 
------------------------------------------------------------------
1    |RVR Time=20130409222756|Reason=FMLP|Desc=TOD_FULL_DAY     |
2    |RVR Time=20130409201134|Reason=FUL |Desc=TOD_FAMILY_LEAVE |

实际结果我想成为

ROW | Val1          | Val2  | Val3         | 
------------------------------------------------------------------
1    |20130409222756|FMLP|TOD_FULL_DAY     |
2    |20130409201134|FUL |TOD_FAMILY_LEAVE |

请帮我解决上述结果。我尝试了子串的子串但不适合我

2 个答案:

答案 0 :(得分:1)

您可以使用regexp_substr()。这是一种方法:

select x.*,
       regexp_substr(regexp_substr(col, 'IVR Time=[^/]*', 1, 1), '[^=]+', 1, 2), 
       regexp_substr(regexp_substr(col, 'Reason=[^/]*', 1, 1), '[^=]+', 1, 2), 
       regexp_substr(regexp_substr(col, 'Desc=[^/]*', 1, 1), '[^=]+', 1, 2)
from (select 'IVR Time=20130409200450CST/Reason=FUL//Desc=TOD_FULL_DAY' as col
      from dual
     ) x

答案 1 :(得分:1)

您可以REGEXP_SUBSTR使用模式(^|/)(.*?)=([^/]*)

SQL Fiddle

Oracle 11g R2架构设置

CREATE TABLE override ( OVR_COMMENT ) AS
SELECT 'RVR Time=20130409200450CST/Reason=FUL/Desc=TOD_FULL_DAY' FROM DUAL UNION ALL
SELECT 'RVR Time=20130409201134/Reason=FUL/Desc=TOD_FAMILY_LEAVE' FROM DUAL

查询1

SELECT REGEXP_SUBSTR( OVR_COMMENT, '(^|/)(.*?)=([^/]*)', 1, 1, NULL, 2 )
         AS key1,
       REGEXP_SUBSTR( OVR_COMMENT, '(^|/)(.*?)=([^/]*)', 1, 1, NULL, 3 )
         AS value1,
       REGEXP_SUBSTR( OVR_COMMENT, '(^|/)(.*?)=([^/]*)', 1, 2, NULL, 2 )
         AS key2,
       REGEXP_SUBSTR( OVR_COMMENT, '(^|/)(.*?)=([^/]*)', 1, 2, NULL, 3 )
         AS value2,
       REGEXP_SUBSTR( OVR_COMMENT, '(^|/)(.*?)=([^/]*)', 1, 3, NULL, 2 )
         AS key3,
       REGEXP_SUBSTR( OVR_COMMENT, '(^|/)(.*?)=([^/]*)', 1, 3, NULL, 3 )
         AS value3
FROM   override

<强> Results

|     KEY1 |            VALUE1 |   KEY2 | VALUE2 | KEY3 |           VALUE3 |
|----------|-------------------|--------|--------|------|------------------|
| RVR Time | 20130409200450CST | Reason |    FUL | Desc |     TOD_FULL_DAY |
| RVR Time |    20130409201134 | Reason |    FUL | Desc | TOD_FAMILY_LEAVE |

查询2

或者您可以使用INSTRSUBSTR

SELECT SUBSTR( OVR_COMMENT, start1, end1 - start1 + 1 ) As value1,
       SUBSTR( OVR_COMMENT, start2, end2 - start2 + 1 ) As value2,
       SUBSTR( OVR_COMMENT, start3, end3 - start3 + 1 ) As value3
FROM   (
  SELECT OVR_COMMENT,
         INSTR( OVR_COMMENT, '=', 1, 1 ) + 1 AS start1,
         INSTR( OVR_COMMENT, '=', 1, 2 ) + 1 AS start2,
         INSTR( OVR_COMMENT, '=', 1, 3 ) + 1 AS start3,
         INSTR( OVR_COMMENT, '/', 1, 1 ) - 1 AS end1,
         INSTR( OVR_COMMENT, '/', 1, 2 ) - 1 AS end2,
         LENGTH( OVR_COMMENT ) AS end3
  FROM   override
)

<强> Results

|            VALUE1 | VALUE2 |           VALUE3 |
|-------------------|--------|------------------|
| 20130409200450CST |    FUL |     TOD_FULL_DAY |
|    20130409201134 |    FUL | TOD_FAMILY_LEAVE |