Oracle- - how to judge whether a string is all numbers

1. Grammar

Realized by regexp_like plus regular expression, for example: judge whether 1000 is a whole number

SELECT 
  'YES' AS IS_NUMBER_FLAG
FROM DUAL
where regexp_like(1000, '^[0-9]+$')

result:

The specific string to be judged can be passed in at the position of 1000, that is, regexp_like(xxx '^[0-9]+$').

Two, use

2.1 Eliminate non-numeric data

If you want to judge whether a column in the database is all numbers, and filter out the non-number rows, you can write like this:

SELECT 
  * 
FROM 
(
  SELECT '1000' DM FROM DUAL UNION ALL
  SELECT '2000' DM FROM DUAL UNION ALL
  SELECT '3000' DM FROM DUAL UNION ALL
  SELECT 'ZZHHFF' DM FROM DUAL UNION ALL
  SELECT 'QQWWEE' DM FROM DUAL UNION ALL
  SELECT '5000' DM FROM DUAL 
) MM
WHERE regexp_like(DM, '^[0-9]+$')

It can be seen from the results that if MM is our data table, the two non-numeric rows have been eliminated.

2.2 Eliminate data that is a number

SELECT 
  * 
FROM 
(
  SELECT '1000' DM FROM DUAL UNION ALL
  SELECT '2000' DM FROM DUAL UNION ALL
  SELECT '3000' DM FROM DUAL UNION ALL
  SELECT 'ZZHHFF' DM FROM DUAL UNION ALL
  SELECT 'QQWWEE' DM FROM DUAL UNION ALL
  SELECT '5000' DM FROM DUAL 
) MM
WHERE regexp_like(DM, '[^0-9]+$')

 

The difference between removing numbers and non-numbers is the use of regular expressions , which are numbers: ^[0-9]+$; non-numbers: [^0-9]+$.

Guess you like

Origin blog.csdn.net/A_captain_608/article/details/127844897