2016-07-26 34 views
0

所以我在我的表LOCMEMBERS中有一個EMAILADDRESS VARCHAR2字段。有些電子郵件地址以10位數的電話號碼開頭,我試圖找出那些有用的電話號碼。所以我正在尋找一種方法來標記[email protected]但不是[email protected]用SQL試圖識別以10位開頭的行

這是一個Oracle數據庫。

我嘗試以下,但SUBSTRING拋出一個錯誤 「無效的標識符」:

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

你嘗試搜索'甲骨文substring'? –

+4

'WHERE REGEXP_LIKE(EMAILADDRESS,'^ [[:digit:]] {10}')' –

+0

如果'EMAILADDRESS'已經是'varchar',那麼將它投射到一個 –

回答

1

試試這個

WITH 
locmembers(member_id, email_address) AS 
(SELECT 1,'[email protected]' FROM dual UNION ALL --numbers but < 10 digits 
    SELECT 2,'[email protected]' FROM dual UNION ALL --numbers = 10 digits 
    SELECT 3,'[email protected]' FROM dual UNION ALL --numbers > 10 digits 
    SELECT 4,'[email protected]' FROM dual UNION ALL 
    SELECT 5,'[email protected]' 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) = '@' 
; 
+0

@Alex K. - 我更新我的回答將您清潔的方式使用REGEXP –

+0

致謝,得到它具有這方面的工作:SELECT member_id ,EMAIL_ADDRESS FROM locmembers WHERE SUBSTR(REGEXP_REPLACE(EMAIL_ADDRESS, '^ [[:數字:]] {10}') ,1,1)='@' –