2017-10-16 72 views
2
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' 

的原始列是分裂使用單varchar列值作爲多列在Oracle SQL(/)字符

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 | 

請幫我解決上述問題。我嘗試子字符串的子字符串,但沒有爲我工作

回答

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

此功能在Oracle 9i中不起作用。請讓我知道如果你已經修復這個 – madhan

+0

查詢2應該在9i工作。 – San

+0

是的,它與9i合作謝謝 – madhan

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

嗨感謝您的回答,但我使用的是Oracle 9i中的REGEXP_SUBSTR不工作就可以了。 – madhan

+0

@madhan。 。 。 Oracle 9i在十多年來一直「不受支持」(http://www.orafaq.com/wiki/Oracle_9i)。 –

相關問題