How to determine whether a string is a digital __oracle in Oracle

Source: Internet
Author: User
Tags translate function
Method One: To judge by To_number function anomaly

CREATE OR REPLACE FUNCTION is_number (
Str_ VARCHAR2) return VARCHAR2
Is
NUM_ number;
BEGIN
Num_: = To_number (STR_);
Return ' Y ';
EXCEPTION
When others THEN
Return ' N ';
End Is_number;
/

Sql> Select Is_number (' 12345 ') from dual;

Is_number (' 12345 ')
--------------------------------------------------------------------------------
Y

Sql> Select Is_number (' 123.45 ') from dual;

Is_number (' 123.45 ')
--------------------------------------------------------------------------------
Y

Sql> Select Is_number (' 123a ') from dual;

Is_number (' 123A ')
--------------------------------------------------------------------------------
N

Sql>

Method two: Using Translate function to realize

SELECT nvl2 (Translate (' 123 ', '/1234567890 ', '/'), ' CHAR ', ' number ')
from dual;

Sql> SELECT nvl2 (Translate (' 123 ', '/1234567890 ', '/'), ' CHAR ', ' number ')
2 from dual;

NVL2 (TRANSLATE (' 123 ', '/1234567
------------------------------
Number

Sql>
Sql> SELECT nvl2 (Translate (' 123A ', '/1234567890 ', '/'), ' CHAR ', ' number ')
2 from dual;

NVL2 (TRANSLATE (' 123A ', '/123456
------------------------------
CHAR

Sql>

In the actual use of the process, can be selected according to the circumstances of these two methods. The advantage of the first method is that you can judge numbers with decimals, and the second method can only judge integers. Because if the Translate function is written in translate (' 123A ', '/', '/', '/') This form will be problematic.

Sql> Select Is_number (' 123.45.6 ') from dual;
 
Is_number (' 123.45.6 ')
----------------------------------------------------------------------- ---------
N
 
sql>
sql> SELECT nvl2 (Translate (' 1234.5.6 ', '/1234567890. ', '/'), ' CHAR ', ' Number ')
  2  from   dual;
&NBSP
NVL2 (TRANSLATE (' 1234.5.6 ', '/12
------------------------------
number
 
Sql>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.