[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

quick PL\SQL  question

Posted on 2004-11-23
7
Medium Priority
?
484 Views
Last Modified: 2012-05-05
Hi Guys,
I need to write a PL\SQL function that would take a string input and tell me if it contains just numbers. It should return true if just numbers (negative ok) and false if its not a number... pretty easy not no clue..

Thanks!
0
Comment
Question by:maxb
7 Comments
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 12657596
Create or Replace Function Is_Number(n varchar2)
Is
V_test Number;
Begin
  V_Test:=n;
  Return 1;
Exceptions
  When INVALID_NUMBER Then
    Return 0;
End;
/
0
 

Author Comment

by:maxb
ID: 12657609
can you walk me through that? comments?
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 12657614
Ooops need o add:

Create or Replace Function Is_Number(n varchar2)
Return Number Is
V_test Number;
Begin
  V_Test:=n;
  Return 1;
Exceptions
  When INVALID_NUMBER Then
    Return 0;
End;
/
Where 1 = TRUE and 0 = FALSE.



0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:oratim
ID: 12657678
create or replace function isNumeric(string IN varchar2)
return boolean
as
temp number;

begin
       temp:=to_number(string);
       return true;
exception
            when others then
            dbms_output.put_line(sqlerrm);
            return false;
end;

To test it:

declare
testa varchar2(100) := 'jklfsa';
testb varchar2(100) := '1234';
res boolean;

begin
       res := isNumeric(testa);
       if res = true then
             dbms_output.put_line('testa is a number');
       else
             dbms_output.put_line('testa is not a number');
      end if;
      
       res := isNumeric(testb);
       if res = true then
             dbms_output.put_line('testb is a number');
       else
             dbms_output.put_line('testb is not a number');
      end if;
end;

output:

testa is not a number
testb is a number


HTH
tim
0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 200 total points
ID: 12657701
Ok what you actually need is to test for VALUE_ERROR:

Create or Replace Function Is_Number(n varchar2)
Return Number Is
V_test Number;
Begin
  V_Test:=n;
  Return 1;
Exception
  When VALUE_ERROR Then
    Return 0;
End;
/

0
 
LVL 3

Expert Comment

by:jaramill
ID: 12657759
Or:

CREATE OR REPLACE FUNCTION is_a_number(p_number VARCHAR2) RETURN BOOLEAN AS

   -- VARIABLES

   l_sql_error_message VARCHAR2(500);

   l_test_number NUMBER;

BEGIN
   
   l_test_number := p_number;  
   RETURN(TRUE);
   
EXCEPTION

   WHEN VALUE_ERROR THEN
   
      RETURN(FALSE);
     
   WHEN OTHERS THEN

      l_sql_error_message := SQLERRM(SQLCODE);
      DBMS_OUTPUT.PUT_LINE(l_sql_error_message);
      RETURN(FALSE);
     
END is_a_number;
/
0
 
LVL 3

Expert Comment

by:jaramill
ID: 12657762
Then test it:

DECLARE

   x BOOLEAN;
   
BEGIN

   x := is_a_number('a');

   DBMS_OUTPUT.ENABLE(5000);
   
   IF(x) THEN
      DBMS_OUTPUT.PUT_LINE('It is a number');      
   ELSE
      DBMS_OUTPUT.PUT_LINE('It is not a number');
   END IF;      

END;
/
0

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Working with Network Access Control Lists in Oracle 11g (part 1) Part 2: http://www.e-e.com/A_9074.html So, you upgraded to a shiny new 11g database and all of a sudden every program that used UTL_MAIL, UTL_SMTP, UTL_TCP, UTL_HTTP or any oth…
Using SQL Scripts we can save all the SQL queries as files that we use very frequently on our database later point of time. This is one of the feature present under SQL Workshop in Oracle Application Express.
This video shows how to recover a database from a user managed backup
This video shows how to configure and send email from and Oracle database using both UTL_SMTP and UTL_MAIL, as well as comparing UTL_SMTP to a manual SMTP conversation with a mail server.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question