Home > Ask the Oracle Experts > PL/SQL Questions & Answers > Parsing in Oracle
Ask The Oracle Expert: Questions & Answers
EMAIL THIS

Parsing in Oracle

Greg Williams EXPERT RESPONSE FROM: Greg Williams

Pose a Question
Other Oracle Categories
Meet all Oracle Experts
Become an Expert for this site


Oracle tips, scripts, and expert advice
Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


>
QUESTION POSED ON: 25 January 2008
I am making a program that will ask the user to enter a customer number. All of the customer numbers are numbers only. If the user enters in a customer number with a letter in it by mistake they will receive an ora-06550 error on that line number. How can I create an exception that will allow me to give the user a nice message to retype the customer number instead of all of the ora error lines? Or, how can I write a line of code that will check the user input to make sure it only contains numbers before Oracle sees it as an error, since you can't make an exception for a compilation error?

Here is my code:
set serveroutput on
begin
 declare
  v_cust_num number := &Customer_Number;
  v_name varchar2(20);
  v_address varchar2(20);
  v_city varchar2(12);
  v_state varchar2(2);
  v_zip varchar2(5);
 begin
  if v_cust_num < 0 then
  raise_application_error (-20000,'Customer number can not be negative');
  else
  select firstname || ' ' || lastname, address, city, state, zip
  into v_name, v_address, v_city, v_state, v_zip
  from customers
  where customer#= v_cust_num;
  dbms_output.put_line(v_name);
  dbms_output.put_line(v_address);
  dbms_output.put_line(v_city || ', '|| v_state ||' '|| v_zip);  end if;  exception
  when no_data_found then
  dbms_output.put_line(chr(10));
  dbms_output.put_line('Customer number entered does not exist.');
  end;
exception
 when value_error or invalid_number then  dbms_output.put_line(chr(10));  dbms_output.put_line('Customer number not entered correctly.'); end;

here is the error if ddd is input by user:
old 3: v_cust_num number := &Customer_Number; new 3: v_cust_num number := ddd; 

  v_cust_num number := ddd;
                       *

ERROR at line 3: 
ORA-06550: line 3, column 24: 
PLS-00201: identifier 'DDD' must be declared
ORA-06550: line 3, column 14: 
PL/SQL: Item ignored
ORA-06550: line 10, column 6: 
PLS-00320: the declaration of the type of this expression is incomplete or malformed
ORA-06550: line 10, column 3: 
PL/SQL: Statement ignored

>
EXPERT RESPONSE
In order to check for characters in the customer number field, you will have to parse the customer number field for characters. As you probably have already found out, Oracle does not have a function that parses a field in 9i. I have created a function called CheckForNumbers (see below). This is something that a lot of shops have or should have. This function accepts a character string and returns a character string delimited by ",". If there are no characters, the function returns the phrase "No Characters". If there are characters, the function returns the characters that are in the string. This function loops thru the string that's passed in and checks to see if the values are between '0' and '9', as follows:

IF to_char(SUBSTR(p_string,i,1)) between '0' and '9' THEN

Notice the single quotes around the 0 and 9. Without the quotes, it would try to compare characters to numbers. This will then generate an error. I hope this solves your problem.

CREATE OR REPLACE function CheckForNumbers (p_string varchar2)
     RETURN varchar2 IS
                
      v_string_len NUMBER;
      v_string VARCHAR2(2000);      
      
BEGIN
      v_string_len := LENGTH(p_string);
      
      FOR  i IN 1..v_string_len LOOP
                
             IF to_char(SUBSTR(p_string,i,1)) between '0' and '9' THEN     
                null;                                                
             else
                v_string := v_string||SUBSTR(p_string, i, 1)||',';    
             end if;    
                  
     END LOOP;
     
      if v_string is null then
         return ('NO Characters');
      else         
         RETURN rtrim(v_string,',');
      end if;
      
               
END CheckForNumbers;


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us    Add to Google


RELATED CONTENT
PL/SQL
Converting Long Raw to Blob
ORA-01422 error when procedure returns more than one row
Definition of force view
ORA-04082: NEW or OLD references not allowed in table level triggers
Execute SQL statement from table in other schema
Script to revoke access from user
PL/SQL procedure to load CSV file into database table
Finding size of files in BLOB datatype
Expression is of wrong type
Sorting a clob column

Oracle development languages
Finding a column value inside a user-supplied string
Update a specific column in a field or row?
Using BETWEEN with DATETIMEs in SQL
Which normal form is used most?
IN list or series of OR conditions?
Connecting tables in a database
SQL query for co-authored books
Querying complex derived tables
Oracle 11g: PL/SQL Basics
SQL string functions

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary



Search and Browse the Expert Answer Center
Search and browse more than 25,000 question and answer pairs from more than 250 TechTarget industry experts.
Browse our Expert Advice

HomeNewsTopicsTipsAsk the ExpertsMultimediaWhite PapersProductsBlogs
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 2003 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts