オラクルのフィールドを分離する方法には、データvar charと数値と日付が含まれますex:
table1:
column1:MohammadBadarneh_07889855_12021990
create procedure to separate and insert into table2:
table2:
name:Mohammad Badarneh
phone number: 07889855
date: 12-02-1990
たとえば、日付形式を検証せずに文字列値を区切るだけで、同様のクエリを作成できます。
-- sample of data
with t1(col) as(
select 'MohammadBadarneh_07889855_12021990' from dual union all
select 'SomethingElse_07889855_12021990' from dual union all
select 'SomethingDifferentHere_07889855_12021990' from dual union all
select 'MohammadBadarneh_07869855_11021990' from dual
),
upos (ps) as(
select level as ps
from dual
connect by level <= 3
),
PData as(
select Name
, Phone
, to_date(Date1, 'dd-mm-yyyy') date1
from (select q.*
, row_number() over(partition by ps order by ps) rn
from (select u.ps
, regexp_substr(col, '[^_]+', 1, u.ps) res
from t1
cross join upos u
) q
) s
pivot (
min(res)
for ps in (1 as Name,2 Phone,3 Date1)
)
)
select Name
, Phone
, Date1
from PData
結果:
Name Phone Date1
---------------------------------------------
MohammadBadarneh 07889855 12-02-1990
SomethingElse 07889855 12-02-1990
MohammadBadarneh 07889855 11-02-1990
SomethingDifferentHere 07869855 12-02-1990
ここにデモがあります
次に、insert
ステートメントを使用して、上記のクエリの出力をテーブルに挿入できます。
insert into your_table_name
select Name
, Phone
, to_date(Date1, 'dd-mm-yyyy')
from (select q.*
, row_number() over(partition by ps order by ps) rn
from (select u.ps
, regexp_substr(t.your_string_column, '[^_]+', 1, u.ps) res
from Yor_Table_With_String t
cross join (select level as ps
from dual
connect by level <= 3
) u
) q
) s
pivot (
min(res)
for ps in (1 as Name,2 Phone,3 Date1)
)
正規表現のみを使用した別のソリューションを次に示します。
with v_data as(
select 'MohammadBadarneh_07889855_12021990' col1 from dual union all
select 'JohnDoe_07869856_01011980' col1 from dual
)
select
regexp_replace(col1, '^([[:alpha:]]+)_([[:digit:]]+)_([[:digit:]]+)', '\1') as name,
regexp_replace(col1, '^([[:alpha:]]+)_([[:digit:]]+)_([[:digit:]]+)', '\2') as phone,
regexp_replace(col1, '^([[:alpha:]]+)_([[:digit:]]+)_([[:digit:]]+)', '\3') as date1
from v_data
同じ正規表現を 3 回使用して、文字列を 3 つの部分 (_ で区切る) に分割します。