Oracle SQL查询以排除包含非数字字符的行

时间:2012-05-09 16:09:01

标签: sql regex oracle

我有以下oracle查询,运行正常

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包含除数字字符以外的任何内容的行。因此上述结果不包括第3行。

2 个答案:

答案 0 :(得分: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 :(得分: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;