检测字符串的一部分是否与另一个字符串匹配

时间:2012-11-05 05:46:31

标签: oracle

大家好,这就是问题:

显示其电子邮件地址中包含完整姓氏的员工的员工ID和电子邮件。

你觉得我能用什么?我可以使用substr吗?如果没有,还有其他建议吗?谢谢你们的和平!

1 个答案:

答案 0 :(得分:0)

我使用正则表达式,例如

CREATE TABLE employees (
  emp_id     NUMBER PRIMARY KEY,
  first_name VARCHAR2(30),
  last_name  VARCHAR2(30),
  email      VARCHAR2(255)
);

INSERT INTO employees VALUES (1, 'Joseph', 'Ratzinger', 'benedictxvi@vatican.va');
INSERT INTO employees VALUES (2, 'Tim', 'Cook', 'tcook@apple.com');
INSERT INTO employees VALUES (3, 'Larry', 'Ellison', 'Ellison@oracle.com');
INSERT INTO employees VALUES (4, 'Michael', 'Dell', 'Michael@dell.com');

SELECT emp_id, email FROM employees WHERE regexp_like(email, last_name);
3 Ellison@oracle.com

要包含与案例无关的结果,请使用:

SELECT emp_id, email FROM employees WHERE regexp_like(email, last_name, 'i');
2 tcook@apple.com
3 Ellison@oracle.com
4 Michael@dell.com

如果您需要排除主机名,请附加' @'模式...

SELECT emp_id, email FROM employees WHERE regexp_like(email, last_name||'@', 'i');
2 tcook@apple.com
3 Ellison@oracle.com

...或从电子邮件中提取主机名:

SELECT emp_id, email 
  FROM employees 
 WHERE regexp_like(regexp_substr(email,'.*@'), last_name, 'i');