根据特定文本拆分字符串-Oracle SQL

时间:2019-03-14 08:45:41

标签: sql oracle

我在Oracle表-table1的一个单元格中有此字符串

MSH|^~\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4
PID|||555-44-4444||EVERYWOMAN^EVE^E^^^^L|JONES|19620320|F|||153 FERNWOOD DR.^^STATESVILLE^OH^35292||(206)3345232|(206)752-121||||AC555444444||67-A4335^OH^20030520
OBR|1|845439^GHH OE|1045813^GHH LAB|15545^GLUCOSE|||200202150730|||||||||
 555-55-5555^PRIMARY^PATRICIA P^^^^MD^^|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
OBX|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
OBX|2|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^172|mg/dl|70_105|H|||F
FT1|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
OBR|2|423525^GHH OE|1045813^GHH LAB|15545^PROTEIN|||200202150730|||||||||
 555-55-5555^PRIMARY^PATRICIA P^^^^MD^^|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
OBX|1|SN|434-5^PROTEIN^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
OBX|2|SN|434-5^PROTEIN1^POST 13H CFST:MCNC:PT:SER/PLAS:QN||^132|mg/dl|70_105|H|||F
OBX|3|SN|434-5^PROTEIN2^POST 14H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
FT1|1|SN|434-5^PROTEIN^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F

我需要在下表中对此进行细分:

table2

SEQ       MESSAGE
1         MSH|^~\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4
          PID|||555-44-4444||EVERYWOMAN^EVE^E^^^^L|JONES|19620320|F|||153 FERNWOOD DR.^^STATESVILLE^OH^35292||(206)3345232|(206)752-121||||AC555444444||67-A4335^OH^20030520
          OBR|1|845439^GHH OE|1045813^GHH LAB|15545^GLUCOSE|||200202150730|||||||||555-55-5555^PRIMARY^PATRICIA P^^^^MD^^|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
          OBX|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
          OBX|2|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^172|mg/dl|70_105|H|||F
          FT1|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F

2         MSH|^~\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4
          PID|||555-44-4444||EVERYWOMAN^EVE^E^^^^L|JONES|19620320|F|||153 FERNWOOD DR.^^STATESVILLE^OH^35292||(206)3345232|(206)752-121||||AC555444444||67-A4335^OH^20030520
          OBR|2|423525^GHH OE|1045813^GHH LAB|15545^PROTEIN|||200202150730|||||||||555-55-5555^PRIMARY^PATRICIA P^^^^MD^^|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
          OBX|1|SN|434-5^PROTEIN^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
          OBX|2|SN|434-5^PROTEIN1^POST 13H CFST:MCNC:PT:SER/PLAS:QN||^132|mg/dl|70_105|H|||F
          OBX|3|SN|434-5^PROTEIN2^POST 14H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
          FT1|1|SN|434-5^PROTEIN^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F

基本上,我需要扫描OBR并中断消息,但始终在消息前添加MSH和PID。

有人可以帮助使用Oracle SQL进行编码吗?

1 个答案:

答案 0 :(得分:2)

Oracle设置

CREATE TABLE test_data ( value ) AS
SELECT 'MSH|^~\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4
PID|||555-44-4444||EVERYWOMAN^EVE^E^^^^L|JONES|19620320|F|||153 FERNWOOD DR.^^STATESVILLE^OH^35292||(206)3345232|(206)752-121||||AC555444444||67-A4335^OH^20030520
OBR|1|845439^GHH OE|1045813^GHH LAB|15545^GLUCOSE|||200202150730|||||||||555-55-5555^PRIMARY^PATRICIA P^^^^MD^^|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
OBX|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
OBX|2|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^172|mg/dl|70_105|H|||F
FT1|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
OBR|2|423525^GHH OE|1045813^GHH LAB|15545^PROTEIN|||200202150730|||||||||555-55-5555^PRIMARY^PATRICIA P^^^^MD^^|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
OBX|1|SN|434-5^PROTEIN^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
OBX|2|SN|434-5^PROTEIN1^POST 13H CFST:MCNC:PT:SER/PLAS:QN||^132|mg/dl|70_105|H|||F
OBX|3|SN|434-5^PROTEIN2^POST 14H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
FT1|1|SN|434-5^PROTEIN^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F'
FROM   DUAL;

查询

SELECT REGEXP_SUBSTR( t.value, '^MSH.*?' || CHR(10) || 'PID.*?' || CHR(10) )
         || REGEXP_SUBSTR( t.value, 'OBR.*?' || CHR(10) || '((OBX|FT1).*?($|' || CHR(10) || '))*', 1, c.COLUMN_VALUE ) AS split_value
FROM   test_data t
       LEFT OUTER JOIN
       TABLE(
         CAST(
           MULTISET(
             SELECT LEVEL
             FROM   DUAL
             CONNECT BY LEVEL <= REGEXP_COUNT( t.value, 'OBR.*?' || CHR(10) || '((OBX|FT1).*?($|' || CHR(10) || '))*' )
           ) AS SYS.ODCINUMBERLIST
         )
       ) c
       ON ( 1 = 1 );

输出

| SPLIT_VALUE                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                      |
| :---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| MSH|^~\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4
| PID|||555-44-4444||EVERYWOMAN^EVE^E^^^^L|JONES|19620320|F|||153 FERNWOOD DR.^^STATESVILLE^OH^35292||(206)3345232|(206)752-121||||AC555444444||67-A4335^OH^20030520
| OBR|1|845439^GHH OE|1045813^GHH LAB|15545^GLUCOSE|||200202150730|||||||||555-55-5555^PRIMARY^PATRICIA P^^^^MD^^|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
| OBX|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
| OBX|2|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^172|mg/dl|70_105|H|||F
| FT1|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
|
| :---------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| MSH|^~\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4
| PID|||555-44-4444||EVERYWOMAN^EVE^E^^^^L|JONES|19620320|F|||153 FERNWOOD DR.^^STATESVILLE^OH^35292||(206)3345232|(206)752-121||||AC555444444||67-A4335^OH^20030520
| OBR|2|423525^GHH OE|1045813^GHH LAB|15545^PROTEIN|||200202150730|||||||||555-55-5555^PRIMARY^PATRICIA P^^^^MD^^|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
| OBX|1|SN|434-5^PROTEIN^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
| OBX|2|SN|434-5^PROTEIN1^POST 13H CFST:MCNC:PT:SER/PLAS:QN||^132|mg/dl|70_105|H|||F
| OBX|3|SN|434-5^PROTEIN2^POST 14H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
| FT1|1|SN|434-5^PROTEIN^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
|

db <>提琴here