使用SQL尝试识别以10位开头的行

时间:2016-07-26 14:50:27

标签: sql oracle

所以我的表EMAILADDRESS VARCHAR2中有一个LOCMEMBERS字段。一些电子邮件地址以10位数的电话号码开头,我正在尝试识别那些电子邮件地址。所以我正在寻找标记5556667777@vtext.com但不是10JamesDean@vtext.com的方法。

这是一个Oracle DB。

我尝试了以下操作,但SUBSTRING引发了错误“无效标识符”:

SELECT SUBSTRING(CAST(EMAILADDRESS AS VARCHAR(20)), 1, 10),         
FROM LOCMEMBERS

1 个答案:

答案 0 :(得分:1)

试试这个

WITH
 locmembers(member_id, email_address) AS
 (SELECT 1,'12345@rrr.com' FROM dual UNION ALL   --numbers but < 10 digits
  SELECT 2,'1122334455@rrr.com' FROM dual UNION ALL  --numbers = 10 digits
  SELECT 3,'112233445566@rrr.com' FROM dual UNION ALL  --numbers > 10 digits
  SELECT 4,'emailaddress@rrr.com' FROM dual UNION ALL
  SELECT 5,'emailsaddress1122334455@rrr.com' FROM dual   --email address with 10 digits
 )
SELECT
 member_id
,email_address
,SUBSTR(REGEXP_REPLACE(email_address, '^[[:digit:]]{10}' ),1,1)
FROM
 locmembers
WHERE SUBSTR(REGEXP_REPLACE(email_address, '^[[:digit:]]{10}' ),1,1) = '@'
;