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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10519898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 00:48:09  来源:igfitidea点击:

Oracle SQL query to exclude rows containing non-numeric characters

sqlregexoracle

提问by Tom

I have the following oracle query which runs fine

我有以下运行良好的 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

and it returns results looking something like this:

它返回看起来像这样的结果:

customercode        sales_id
12345678            456
34534534            678
23423423            X123

What I would like to do is exclude rows where the sales_id contains anything other than numeric characters. So the above results would not include the 3rd row.

我想要做的是排除 sales_id 包含数字字符以外的任何内容的行。所以上面的结果不包括第三行。

回答by Justin Cave

You can use regular expressions. In this case, regexp_like( sales_id, '^[[:digit:]]*$' )

您可以使用正则表达式。在这种情况下,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

回答by DCookie

TRANSLATEwill work, too:

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;