我在Oracle SQL的一个表的单元格中有一个文本:
ISA*00*AUTHORIZAT*00*SECURITY I*ZZ*000000060000000*ZZ*000000010000000*110705*1132*^*00501*110705001*0*T*:~
GS*HC*00000006*00000001*20110705*113253*110705001*X*005010X222A1~
ST*837*0021*005010X222~
BHT*0019*00*244579*20061015*1023*CH~
NM1*41*2*PREMIER BILLING SERVICE*****46*TGJ23~
PER*IC*JERRY*TE*3055552222*EX*231~
NM1*40*2*KEY INSURANCE COMPANY*****46*66783JJT~
HL*1**20*1~
PRV*BI*PXC*203BF0100Y~
NM1*85*2*BEN KILDARE SERVICE*****XX*9876543210~
N3*234 SEAWAY ST~
N4*MIAMI*FL*33111~
请注意,以上内容位于1个单元格中。我需要将其分解为多行,并为其分配一个循环。
表格如下:
loop message
000 ISA*00*AUTHORIZAT*00*SECURITY I*ZZ*000000060000000*ZZ*000000010000000*110705*1132*^*00501*110705001*0*T*:~
000 GS*HC*00000006*00000001*20110705*113253*110705001*X*005010X222A1~
000 ST*837*0021*005010X222~
000 BHT*0019*00*244579*20061015*1023*CH~
1000A NM1*41*2*PREMIER BILLING SERVICE*****46*TGJ23~
1000A PER*IC*JERRY*TE*3055552222*EX*231~
1000B NM1*40*2*KEY INSURANCE COMPANY*****46*66783JJT~
2000A HL*1**20*1~
2010AA PRV*BI*PXC*203BF0100Y~
2010AA NM1*85*2*BEN KILDARE SERVICE*****XX*9876543210~
2010AA N3*234 SEAWAY ST~
2010AA N4*MIAMI*FL*33111~
因此,基本上,我需要根据新行分割字符串,然后根据序列分配适当的循环。规则是:
我能够使用正则表达式成功地将文本分成多行,但无法根据条件分配值。
是否可以在Oracle SQL中对其进行编码?
答案 0 :(得分:1)
Oracle设置:
CREATE TABLE test_data ( value ) AS
SELECT 'ISA*00*AUTHORIZAT*00*SECURITY I*ZZ*000000060000000*ZZ*000000010000000*110705*1132*^*00501*110705001*0*T*:~
GS*HC*00000006*00000001*20110705*113253*110705001*X*005010X222A1~
ST*837*0021*005010X222~
BHT*0019*00*244579*20061015*1023*CH~
NM1*41*2*PREMIER BILLING SERVICE*****46*TGJ23~
PER*IC*JERRY*TE*3055552222*EX*231~
NM1*40*2*KEY INSURANCE COMPANY*****46*66783JJT~
HL*1**20*1~
PRV*BI*PXC*203BF0100Y~
NM1*85*2*BEN KILDARE SERVICE*****XX*9876543210~
N3*234 SEAWAY ST~
N4*MIAMI*FL*33111~'
FROM DUAL;
查询:
WITH rsqfc ( value, line, rn, max_rn, nm1, prv, depth ) AS (
SELECT value,
REGEXP_SUBSTR( value, '.+?(' || CHR(10) || '|$)', 1, 1 ),
1,
REGEXP_COUNT( value, '.+?(' || CHR(10) || '|$)' ) - 1,
CASE SUBSTR( value, 1, 4 ) WHEN 'NM1*' THEN 1 ELSE 0 END,
CASE SUBSTR( value, 1, 4 ) WHEN 'PRV*' THEN 1 ELSE 0 END,
CASE SUBSTR( value, 1, 4 ) WHEN 'NM1*' THEN 1 WHEN 'PRV*' THEN 2 ELSE 0 END
FROM test_data
UNION ALL
SELECT value,
REGEXP_SUBSTR( value, '.+?(' || CHR(10) || '|$)', 1, rn + 1 ),
rn + 1,
max_rn,
CASE SUBSTR( REGEXP_SUBSTR( value, '.+?(' || CHR(10) || '|$)', 1, rn + 1 ), 1, 4 ) WHEN 'NM1*' THEN nm1 + 1 ELSE nm1 END,
CASE SUBSTR( REGEXP_SUBSTR( value, '.+?(' || CHR(10) || '|$)', 1, rn + 1 ), 1, 4 ) WHEN 'PRV*' THEN prv + 1 ELSE prv END,
CASE SUBSTR( REGEXP_SUBSTR( value, '.+?(' || CHR(10) || '|$)', 1, rn + 1 ), 1, 4 ) WHEN 'NM1*' THEN 1 WHEN 'PRV*' THEN 2 ELSE depth END
FROM rsqfc
WHERE rn < max_rn
)
SELECT CASE depth
WHEN 2 THEN '2010A' || CHR( 64 + prv )
WHEN 1 THEN '1000' || CHR( 64 + nm1 )
WHEN 0 THEN '000'
END AS "LOOP",
line
FROM rsqfc;
输出:
LOOP | LINE :----- | :------------------------------------------------------------------------------------------------------------- 000 | ISA*00*AUTHORIZAT*00*SECURITY I*ZZ*000000060000000*ZZ*000000010000000*110705*1132*^*00501*110705001*0*T*:~<br> 000 | GS*HC*00000006*00000001*20110705*113253*110705001*X*005010X222A1~<br> 000 | ST*837*0021*005010X222~<br> 000 | BHT*0019*00*244579*20061015*1023*CH~<br> 1000A | NM1*41*2*PREMIER BILLING SERVICE*****46*TGJ23~<br> 1000A | PER*IC*JERRY*TE*3055552222*EX*231~<br> 1000B | NM1*40*2*KEY INSURANCE COMPANY*****46*66783JJT~<br> 1000B | HL*1**20*1~<br> 2010AA | PRV*BI*PXC*203BF0100Y~<br> 1000C | NM1*85*2*BEN KILDARE SERVICE*****XX*9876543210~<br> 1000C | N3*234 SEAWAY ST~<br>
db <>提琴here