2012-05-09 307 views
0

行我有它運行以下罰款Oracle查詢的Oracle SQL查詢來排除包含非數字字符

SELECT c.customercode, s.sales_id 
FROM customers c 
LEFT JOIN sales s ON c.customercode = s.sales_id 
WHERE c.address IS NOT NULL 

,並返回結果看起來像這樣:

customercode  sales_id 
12345678   456 
34534534   678 
23423423   X123 

我想要排除sales_id包含除數字字符以外的任何行的行。所以上面的結果不會包含第三行。

回答

4

您可以使用正則表達式。在這種情況下,regexp_like(sales_id, '^[[:digit:]]*$')

SQL> with x as (select 12345678 code, '456' sales_id from dual union all 
    2    select 34534534, '678' from dual union all 
    3    select 23423423, 'X123' from dual) 
    4 select * 
    5 from x 
    6 where regexp_like(sales_id, '^[[:digit:]]*$'); 

     CODE SALE 
---------- ---- 
    12345678 456 
    34534534 678 
1

TRANSLATE會工作,太:

WITH q AS (
SELECT '12345678' customercode,'456' sales_id FROM dual 
UNION ALL 
SELECT '34534534','678' FROM dual 
UNION ALL 
SELECT '23423423','X123' FROM dual 
) 
SELECT * 
    FROM q 
WHERE TRANSLATE(sales_id,'A1234567890','A') IS NULL; 
相關問題