我有一个称为目录的表:
create table contents(file_name varchar2(4000), file_content clob);
这是桌子:
file_name file_content
deID.RESUL_12433287659.txt_234323456.txt |678976|TEST|TBDKK|7865679809
deID.RESUL_34534563649.txt_345353567.txt 1|678977||TB5KK|7866709
deID.RESUL_44235345636.txt_537967875.txt |678978|TE2T|TB4KK|78669809
deID.RESUL_35234663456.txt_423452545.txt 4|678979|TE3T|T3DKK|785679809
我需要使用具有以下结构的内容创建另一个名为data_contents的表:
file_name id number name address phone
deID.RESUL_12433287659.txt_234323456.txt 678976 TEST TBDKK 7865679809
deID.RESUL_34534563649.txt_345353567.txt 1 678977 TB5KK 7866709
deID.RESUL_44235345636.txt_537967875.txt 678978 TE2T TB4KK 78669809
deID.RESUL_35234663456.txt_423452545.txt 4 678979 TE3T T3DKK 785679809
SQL中是否有任何方法可以分隔分隔的管道并为其分配特定的列?
答案 0 :(得分:2)
使用regexp_substr,并填充没有ID的行:
with DTE as
(
select file_name,
to_char(file_content) as file_content -- preconvert the clob to a varchar
from MyTable
)
, CTE as
(
select file_name,
case
when substr(file_content,1,1) ='|' -- If the string starts with the delimiter
then ' '||file_content -- then add a space at the start
else file_content
end as file_content
from DTE
)
select file_name,
regexp_substr (file_content, '[^|]+',1, 1 ) as id,
regexp_substr (file_content, '[^|]+',1, 2 ) as thenumber,
regexp_substr (file_content, '[^|]+',1, 3 ) as thename,
regexp_substr (file_content, '[^|]+',1, 4 ) as theaddress,
regexp_substr (file_content, '[^|]+',1, 5) as phone
from CTE