The fastest way is to replace all digits with null (to simply delete them) and see if anything is left. You don't need regular expressions (slow!) for this, you just need the standard string function TRANSLATE()
.
Unfortunately, Oracle has to work around their own inconsistent treatment of NULL
- sometimes as empty string, sometimes not. In the case of the TRANSLATE()
function, you can't simply translate every digit to nothing; you must also translate a non-digit character to itself, so that the third argument is not an empty string (which is treated as a real NULL
, as in relational theory). See the Oracle documentation for the TRANSLATE()
function. https://docs.oracle.com/cd/E11882_01/server.112/e41084/functions216.htm#SQLRF06145
Then, the result can be obtained with a CASE
expression (or various forms of NULL
handling functions; I prefer CASE
, which is SQL Standard):
with
nums ( num ) as (
select '123' from dual union all
select '-56' from dual union all
select '7A9' from dual union all
select '0.9' from dual
)
-- End of simulated inputs (for testing only, not part of the solution).
-- SQL query begins BELOW THIS LINE. Use your own table and column names.
select num,
case when translate(num, 'z0123456789', 'z') is null
then num
else '0'
end as result
from nums
;
NUM RESULT
--- ------
123 123
-56 0
7A9 0
0.9 0
Note: everything here is in varchar2
data type (or some other kind of string data type). If the results should be converted to number, wrap the entire case expression within TO_NUMBER()
. Note also that the strings '-56'
and '0.9'
are not all-digits (they contain non-digits), so the result is '0'
for both. If this is not what you needed, you must correct the problem statement in the original post.