If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Oracle > PL/SQL - How can I verify presence of alphanumeric chars in a text field

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-27-10, 05:34
MxC MxC is offline
Registered User
 
Join Date: Aug 2010
Posts: 6
PL/SQL - How can I verify presence of alphanumeric chars in a text field

I've a text column in my table. This col contains mostly numbers, but in some cases can contain alphanumeric value also. In my PL/SQL code, amongst other things, I would like to check if this particular col has such alphanumeric value in case of which I'd like to take certain action, else continue normal flow of program.

Any suggestions on how it could be done?
Reply With Quote
  #2 (permalink)  
Old 08-27-10, 06:02
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
You can use regexp_like to only retrieve the rows you are interested in:

http://download-uk.oracle.com/docs/c...7.htm#i1048942

Code:
SELECT *
FROM the_unknown_table
WHERE REGEXP_LIKE(the_unknow_column, '^[0-9]+$');
or inside a PL/SQL block
Code:
  if regexp_like(some_value, '^[0-9]+$') then
     dbms_output.put_line('number');
  else
     dbms_output.put_line('not a number');
  end if;

Last edited by shammat; 08-27-10 at 06:05. Reason: Added PL/SQL example
Reply With Quote
  #3 (permalink)  
Old 08-27-10, 06:50
MxC MxC is offline
Registered User
 
Join Date: Aug 2010
Posts: 6
Thanks, that's quite useful. Unfortunately it seems it works 10g onwards. I'm having 9i

Any other suggestions that could work in 9i?
Reply With Quote
  #4 (permalink)  
Old 08-27-10, 07:15
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,407
Quote:
Originally Posted by MxC View Post
Thanks, that's quite useful. Unfortunately it seems it works 10g onwards. I'm having 9i

Any other suggestions that could work in 9i?
Wow, that is an ancient version.
(That'll teach you to include your version number, especially when using a non-supported version )


The only workaround I can see, is to create a function that tries to convert a string to a number and catches all exceptions. If an exception occurred you ignore it and signal that it was not a number, something like:

Code:
create or replace function safe_to_number(to_test varchar)
return integer
is
  result integer;
begin
  begin
    result := to_number(to_test);
  exception when others then
    result := null;
  end;
  return result;
end;
/
You will need to adjust the to_number() call to use your expected number format.

But be warned: this is not a really fast solution, especially when you have a lot of exceptions!
Reply With Quote
  #5 (permalink)  
Old 08-27-10, 08:23
MxC MxC is offline
Registered User
 
Join Date: Aug 2010
Posts: 6
Thanks, that is a useful suggestion indeed.
BTW, the lesson was learnt immediately when realized that your solution worked only on 10g
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On