dbTalk Databases Forums  

basic plsql question

comp.databases.oracle comp.databases.oracle


Discuss basic plsql question in the comp.databases.oracle forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jmaxsherkimer
 
Posts: n/a

Default basic plsql question - 08-27-2004 , 07:20 AM






i had a basic question on plsql, if i have a variable as varchar2,
containing some text like 'A12345', how do i write a function to make
sure that the first char. is the letter 'A' or 'a' and the next five
characters are numbers? sorry about asking a basic question, but i'm
not the dba here, he recently left and i'm just the web developer
having to do this for now.

Reply With Quote
  #2  
Old   
Jim Kennedy
 
Posts: n/a

Default Re: basic plsql question - 08-27-2004 , 08:38 AM







"jmaxsherkimer" <jmaxsherkimer (AT) hotmail (DOT) com> wrote

Quote:
i had a basic question on plsql, if i have a variable as varchar2,
containing some text like 'A12345', how do i write a function to make
sure that the first char. is the letter 'A' or 'a' and the next five
characters are numbers? sorry about asking a basic question, but i'm
not the dba here, he recently left and i'm just the web developer
having to do this for now.
Look in the docs you can find documentation at otn.oracle.com. Look in the
SQL Reference guide. (there are a ton of docs, but that is the one you want)
You will probably use substr and translate. You can test it in sqlplus by


select substr('A12345',1,1) from dual;

etc.
That will allow you to evaluate the expression so you can test your function
quickly. Once you are satisfied with your exprerssion you can put the
expression in your code.
Jim




Reply With Quote
  #3  
Old   
Wario
 
Posts: n/a

Default Re: basic plsql question - 08-27-2004 , 01:06 PM



This function will let you test the format any string up to 50
characters. You may modify is needed.

function word_format (word varchar2) return varchar2 is
fmt varchar2(50);
ltr char(1);
begin
for x in 1..length(word) loop
ltr := substr(word,x,1);
fmt := fmt || case
when ltr between 'A' and 'Z' then 'X'
when ltr between 'a' and 'z' then 'X'
when ltr between '0' and '9' then '9'
else ltr
end;
end loop;

return fmt;
end;
/

if word_format('A54698') = 'X99999' then
.....
end if;

Reply With Quote
  #4  
Old   
Wario
 
Posts: n/a

Default Re: basic plsql question - 08-30-2004 , 02:36 PM



This procedure is better. You could use the translate function alone,
but the string parameters would make your code long and unwieldy.

create function word_fmt (word varchar2) return varchar2 is
begin
return translate(word,
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789',
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX9999999999'
);
end;

if word_fmt('a52132') = 'X99999' then
.....
end if;

or

if translate(word, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789',
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX9999999999') =
'X99999' then
....
end if;

Reply With Quote
  #5  
Old   
Pedro Lopes
 
Posts: n/a

Default Re: basic plsql question - 08-30-2004 , 06:12 PM



You can use regular expressions on 10g.

see the documentation at http://tahiti.oracle.com

bye,
pedro


jmaxsherkimer wrote:
Quote:
i had a basic question on plsql, if i have a variable as varchar2,
containing some text like 'A12345', how do i write a function to make
sure that the first char. is the letter 'A' or 'a' and the next five
characters are numbers? sorry about asking a basic question, but i'm
not the dba here, he recently left and i'm just the web developer
having to do this for now.

Reply With Quote
  #6  
Old   
Alex Filonov
 
Posts: n/a

Default Re: basic plsql question - 08-31-2004 , 02:52 PM



r0cky (AT) insightbb (DOT) com (Wario) wrote in message news:<9204a53e.0408301136.51b33314 (AT) posting (DOT) google.com>...
Quote:
This procedure is better. You could use the translate function alone,
but the string parameters would make your code long and unwieldy.

create function word_fmt (word varchar2) return varchar2 is
begin
return translate(word,
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789',
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX9999999999'
);
end;

First character should be 'a' or 'A', other four characters are digits,
so translate function call can look like

translate(word, 'aA0123456789', 'AA9999999999')


Quote:
if word_fmt('a52132') = 'X99999' then
.....
end if;

or

if translate(word, 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVW XYZ0123456789',
'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX XXX9999999999') =
'X99999' then
....
end if;

Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.