テーブル account -------------------- Andrew.Hide@aaa.com Frank.Bishop@aaa.com Gandi.Malik@aaa.com George.Bisk@aaa.com Martin.Loe@aaa.com First_nameとLast_nameを出力する。 出力結果 account First_name Last_name -------------------- ---------- --------- Andrew.Hide@aaa.com Andrew Hide Frank.Bishop@aaa.com Frank Bishop Gandi.Malik@aaa.com Gandi Malik George.Bisk@aaa.com George Bisk Martin.Loe@aaa.com Martin Loe
create table mailTable(account) as select 'Frank.Bishop@aaa.com' from dual union select 'George.Bisk@aaa.com' from dual union select 'Andrew.Hide@aaa.com' from dual union select 'Martin.Loe@aaa.com' from dual union select 'Gandi.Malik@aaa.com' from dual;
col First_name for a10 col Last_name for a10 --■■■正規表現を使う方法1(10g以降)■■■ select account, RegExp_Replace(account,'^([^.]+).*$','\1') as First_name, RegExp_Replace(account,'^[^.]+\.([^@]+).*$','\1') as Last_name from mailTable; --■■■正規表現を使う方法2(10g以降)■■■ select account, RegExp_Substr(account,'^[^.]+') as First_name, RegExp_Replace(account,'^[^.]+\.([^@]+).*$','\1') as Last_name from mailTable; --■■■正規表現を使わない方法■■■ select account, substr(account,1,instr(account,'.')-1) as First_name, substr(account,instr(account,'.')+1,instr(account,'@')-instr(account,'.')-1) as Last_name from mailTable;
正規表現の 最小マッチと、先読みと、戻り読み(可変長)が使えるなら、 First_nameは、^.+?(?=\.) Last_nameは、(?<=^.+?\.).+?(?=@) で取得できます。