简体   繁体   English

如何在 Oracle SQL 中仅使用数字字符的 select 行

[英]How to select rows with only Numeric Characters in Oracle SQL

I would like to keep rows only with Numeric Character ie 0-9.我想只保留带有数字字符的行,即 0-9。 My source data can have any type of character eg 2,%,(.我的源数据可以有任何类型的字符,例如 2,%,(.

Input (postcode)
3453gds sdg3
454232
sdg(*d^
452

Expected Output (postcode)
454232
452

I have tried using WHERE REGEXP_LIKE(postcode, '^[[:digit:]]+$');我试过使用WHERE REGEXP_LIKE(postcode, '^[[:digit:]]+$'); however in my version of Oracle I get an error saying但是在我的 Oracle 版本中,我收到一条错误消息

function regexp_like(character varying, "unknown") does not exist function regexp_like(字符变化,“未知”)不存在

For Oracle 10 or higher you can use regexp functions.对于Oracle 10或更高版本,您可以使用正则regexp函数。 In earlier versions translate function will help you:在早期版本中翻译 function 将帮助您:

SELECT  postcode
FROM    table_name
WHERE   length(translate(postcode,'0123456789','1')) is null
AND     postcode IS NOT NULL;

OR或者

SELECT translate(postcode, '0123456789' || translate(postcode,'x123456789','x'),'0123456789') nums
   FROM table_name  ;

the above answer also works for me上面的答案也适用于我

SELECT translate('1234bsdfs3@23##PU', '0123456789' || translate('1234bsdfs3@23##PU','x123456789','x'),'0123456789') nums
   FROM dual  ;

Nums:
1234323

You want regexp_like() and your version should work:您想要regexp_like()并且您的版本应该可以工作:

select t.*
from t
where regexp_like(t.postcode, '^[0-9]+$');

However, your error looks more like a Postgres error, so perhaps this will work:但是,您的错误看起来更像是 Postgres 错误,所以也许这会起作用:

t.postcode ~ '^[0-9]+$'

For an alternative to the Gordon Linoff answer, we can try using REGEXP_REPLACE :对于 Gordon Linoff 答案的替代方案,我们可以尝试使用REGEXP_REPLACE

SELECT *
FROM yourTable
WHERE REGEXP_REPLACE(postcode, '[0-9]+', '') IS NULL;

The idea here is to strip away all digit characters, and then assert that nothing were left behind.这里的想法是去掉所有数字字符,然后断言没有留下任何东西。 For a mixed digit-letter value, the regex replacement would result in a non-empty string.对于混合的数字字母值,正则表达式替换将导致非空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM