我只想保留数字字符(即0-9)的行。我的源数据可以具有任何类型的字符,例如2,%,(。
Input (postcode)
3453gds sdg3
454232
sdg(*d^
452
Expected Output (postcode)
454232
452
我尝试使用WHERE REGEXP_LIKE(postcode,'^[[:digit:]]+$');
但是在我的Oracle版本中,我收到一条错误消息
函数regexp_like(字符变化,“未知”)不存在
我只想保留数字字符(即0-9)的行。我的源数据可以具有任何类型的字符,例如2,%,(。
Input (postcode)
3453gds sdg3
454232
sdg(*d^
452
Expected Output (postcode)
454232
452
我尝试使用WHERE REGEXP_LIKE(postcode,'^[[:digit:]]+$');
但是在我的Oracle版本中,我收到一条错误消息
函数regexp_like(字符变化,“未知”)不存在
对于Oracle 10
或更高版本,您可以使用regexp
函数。在早期版本中,翻译功能将为您提供帮助:
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 ;
上面的答案也对我有用
SELECT translate('1234bsdfs3@23##PU','0123456789' || translate('1234bsdfs3@23##PU','0123456789') nums
FROM dual ;
Nums:
1234323
,
您想要regexp_like()
,并且您的版本应该可以运行:
select t.*
from t
where regexp_like(t.postcode,'^[0-9]+$');
但是,您的错误看起来更像是Postgres错误,因此也许可以解决:
t.postcode ~ '^[0-9]+$'
,
对于Gordon Linoff答案的替代方案,我们可以尝试使用REGEXP_REPLACE
:
SELECT *
FROM yourTable
WHERE REGEXP_REPLACE(postcode,'[0-9]+','') IS NULL;
这里的想法是剥离所有数字字符,然后断言没有留下任何东西。对于混合的数字字母值,正则表达式替换将导致非空字符串。