Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


how to write PL/SQL function

Posted on 2008-10-02
Medium Priority
Last Modified: 2013-12-07
How to do a pl/sql function that takes two strings representing a list of
numbers separated by commas and returns a string representing the list
of each nth element added together.  You don't know how long the list
will be, but assume it has to fit in a PL/SQL varchar2.


      add_nums('1,2,3,4','3,4,5,6') -> '4,6,8,10'
      add_nums('1,2,3','15,14,13') -> '16,16,16'

It should fail gracefully when the lengths of the two lists are not
Question by:wasabi3689
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 32

Accepted Solution

awking00 earned 360 total points
ID: 22625590
See attached.
LVL 27

Expert Comment

ID: 22626390
A neater version:

create or replace function test_func(p_arg1 varchar2, p_arg2 varchar2)
return varchar2
 if ( instr(p_arg1,',') = 0 and instr(p_arg2,',') = 0 ) then
  return to_number(p_arg1) + to_number(p_arg2);
 elsif (instr(p_arg1,',') = 0 OR instr(p_arg2,',') = 0) then
  raise_application_error(-20001, 'Length of the strings are not equal');
  return to_char(to_number(substr(p_arg1, 1, instr(p_arg1,',') - 1)) + to_number(substr(p_arg2, 1, instr(p_arg2,',') - 1)))
         test_func(substr(p_arg1, instr(p_arg1,',') + 1 ), substr(p_arg2, instr(p_arg2,',') + 1 ));
 end if;

Expert Comment

ID: 22654602
We can achive this in SQL itself. pls check the below SQL. In this we have to provide two string with comma delimeter. Sting should not end with comma. we can give any length of string lk
str = 1,2,3,4 str1=4,5,6,7 op=5,7,9,10,7
str=1,2,3,4,5 str1=2,3,4,5 op=3,5,7,9,5
str=1,2,3,4 str1=3,4,5,6 op=4,6,8,10

select op from (
select r, substr(sys_connect_by_path(tot, ','),2) op
from (
select r, sum(a)+sum(b) tot
from (
select rownum r, to_number(decode(rownum, 1, substr('&&str', 1, instr('&&str',',',1)-1),
                      length(translate('&&str', ',1234567890', ','))+1, substr('&&str',instr('&&str', ',',1,rownum-1)+1,length('&&str')),
                      substr('&&str', instr('&&str', ',',1,rownum-1)+1, (instr('&&str',',',1, rownum)-1 - instr('&&str', ',',1,rownum-1))))) a, 0 b
from all_objects
where rownum <= length(translate('&&str', ',1234567890', ','))+1
select rownum r, 0 a, to_number(decode(rownum, 1, substr('&&str1', 1, instr('&&str1',',',1)-1),
                      length(translate('&&str1', ',1234567890', ','))+1, substr('&&str1',instr('&&str1', ',',1,rownum-1)+1,length('&&str1')),
                      substr('&&str1', instr('&&str1', ',',1,rownum-1)+1, (instr('&&str1',',',1, rownum)-1 - instr('&&str1', ',',1,rownum-1))))) b
from all_objects
where rownum <= length(translate('&&str1', ',1234567890', ','))+1)
group by r) x
start with r=1
connect by prior r = r-1
order by r desc)
where rownum < 2

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Why doesn't the Oracle optimizer use my index? Querying too much data Most Oracle developers know that an index is useful when you can use it to restrict your result set to a small number of the total rows in a table. So, the obvious side…
Background In several of the companies I have worked for, I noticed that corporate reporting is off loaded from the production database and done mainly on a clone database which needs to be kept up to date daily by various means, be it a logical…
This video shows how to copy a database user from one database to another user DBMS_METADATA.  It also shows how to copy a user's permissions and discusses password hash differences between Oracle 10g and 11g.
This video shows setup options and the basic steps and syntax for duplicating (cloning) a database from one instance to another. Examples are given for duplicating to the same machine and to different machines

704 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