我有一个名为 email_id 的表,列名称为 email_id ,并且这些电子邮件ID位于
列中name@yahoo.com
name@msn.com
name@gmail.com
name@xmsoftware.com
我只需要这样的域名,例如结果
yahoo
msn
gmail
xmsoftware
是否有最简单的方法(或其他方法)?
select substr(email_id.email_id,(instr(email_id.email_id,'@',1)+1),(instr(email_id.email_id,'.',1) - (instr(email_id.email_id,'@',1)+1))) as domain
from email_id;
答案 0 :(得分:4)
我希望使用REGEXP_SUBSTR
select REGEXP_SUBSTR(email_id,'^.*@(.*)\.(.*)$',1,1,null,1) as domain
FROM emails;-- ^ first match ^ within()
答案 1 :(得分:1)
老式方法是使用SUBSTR + INSTR
组合。与正则表达式相比,它需要输入更多的字母,但是在大型数据集上可能会更快地工作。
SQL> WITH email_id (email_id)
2 AS (SELECT 'name@yahoo.com' FROM DUAL
3 UNION ALL
4 SELECT 'name@msn.com' FROM DUAL
5 UNION ALL
6 SELECT 'name@gmail.com' FROM DUAL
7 UNION ALL
8 SELECT 'name@xmsoftware.com ' FROM DUAL)
9 select email_id,
10 substr(email_id,
11 instr(email_id, '@') + 1,
12 instr(email_id, '.', instr(email_id, '@')) - instr(email_id, '@') - 1) result
13 from email_id;
EMAIL_ID RESULT
-------------------- --------------------
name@yahoo.com yahoo
name@msn.com msn
name@gmail.com gmail
name@xmsoftware.com xmsoftware
SQL>